473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Strange abortion of for loop

Hi.

I've come across someting strange. I was trying to make a for-loop
execute repetadly until the function called inside it does not return
true during the entire loop (see program below).

The two lines that confuse me are marked as (1) and (2).

count=0;
bool s(true);
while(s){
s=false;
for (int x=0; x<10; ++x){
//s = s||f(x,count); //(1)
bool tmp =f(x, count); s=s||tmp; //(2)
}
count ++;
}
where f(x,count) is a bool function, returning true for count<3

I thought that they both do the same, ie fun() is executed and bool
variable s = s OR (return from function). However, line (1) will abort
the execution of the for loop already when x=0, whereas line (2) will
let the for loop executs f() 10 times.

Can anyone explain what is going on here and why the two lines don't
behave the same way? Is this the result of some optimization (where the
compiler only looks at changes of variable s and not the side effects of
the function call?)

Full program and outputs for the two different lines are below

regards
/hall

Program code and Outputs
--------------------------

Please scroll down to see the outputs from this program

//-----------------
#include <iostream>
using namespace std;

bool f(int x, int c){
cout << "f("<<x<<","<<c<<")";
return c<3;
}

int main(int argc, char* argv[])
{

bool s(true);
int count(0);

while(s){
s=false;

for (int x=0; x<10; ++x){
//s = s||f(x,count); //(1)
bool tmp =f(x, count); s=s||tmp; //(2)
}

count ++; cout << ", s="<< s<<endl;
}

char a; cin >> a;
return 0;
}
// -------------------
Outputs from program

Case 1) Line (1) is commented out, Output is what I expect:
-----------------
f(0,0)f(1,0)f(2,0)f(3,0)f(4,0)f(5,0)f(6,0)f(7,0)f( 8,0)f(9,0), s=1
f(0,1)f(1,1)f(2,1)f(3,1)f(4,1)f(5,1)f(6,1)f(7,1)f( 8,1)f(9,1), s=1
f(0,2)f(1,2)f(2,2)f(3,2)f(4,2)f(5,2)f(6,2)f(7,2)f( 8,2)f(9,2), s=1
f(0,3)f(1,3)f(2,3)f(3,3)f(4,3)f(5,3)f(6,3)f(7,3)f( 8,3)f(9,3), s=0
-----------------

Case 2) Line (2) is commented out, (1) is not. The for loop gets
executed only once when f() returns true.
-----------------
f(0,0), s=1
f(0,1), s=1
f(0,2), s=1
f(0,3)f(1,3)f(2,3)f(3,3)f(4,3)f(5,3)f(6,3)f(7,3)f( 8,3)f(9,3), s=0
-----------------


--
( - Remove capital X from email to reply - )

Jul 22 '05 #1
4 1408
On Wed, 14 Apr 2004 16:19:19 +0200, hall <Xc***********@yahoo.se> wrote:
Hi.

I've come across someting strange. I was trying to make a for-loop
execute repetadly until the function called inside it does not return
true during the entire loop (see program below).

The two lines that confuse me are marked as (1) and (2).

count=0;
bool s(true);
while(s){
s=false;
for (int x=0; x<10; ++x){
//s = s||f(x,count); //(1)
bool tmp =f(x, count); s=s||tmp; //(2)
}
count ++;
}
where f(x,count) is a bool function, returning true for count<3

I thought that they both do the same, ie fun() is executed and bool
variable s = s OR (return from function). However, line (1) will abort
the execution of the for loop already when x=0, whereas line (2) will
let the for loop executs f() 10 times.

Can anyone explain what is going on here and why the two lines don't
behave the same way? Is this the result of some optimization (where the
compiler only looks at changes of variable s and not the side effects of
the function call?)
Just from what you've said, it seems pretty clear you're not familiar with
the "short circuit" behavior of the || and && operators. In both cases, if
evaluation of the left-hand operand results in a value that "forces" the
logical result of the entire expression, then the right-hand operand is
guaranteed to NOT be evaluated. Otherwise, the right-hand operand is
evaluated and its value is the result of the binary expression.

In your (1), then, f() will not even be called if s is true (and in that
case, s will remain true). If s were false, then s's new value becomes the
result of the call to f().

In your (2), you're calling f unconditionally in the first part, and the
lack of side-effects in the second part means there's nothing surprising
that can happen /there/ either.
-leor

Full program and outputs for the two different lines are below

regards
/hall

Program code and Outputs
--------------------------

Please scroll down to see the outputs from this program

//-----------------
#include <iostream>
using namespace std;

bool f(int x, int c){
cout << "f("<<x<<","<<c<<")";
return c<3;
}

int main(int argc, char* argv[])
{

bool s(true);
int count(0);

while(s){
s=false;

for (int x=0; x<10; ++x){
//s = s||f(x,count); //(1)
bool tmp =f(x, count); s=s||tmp; //(2)
}

count ++; cout << ", s="<< s<<endl;
}

char a; cin >> a;
return 0;
}
// -------------------
Outputs from program

Case 1) Line (1) is commented out, Output is what I expect:
-----------------
f(0,0)f(1,0)f(2,0)f(3,0)f(4,0)f(5,0)f(6,0)f(7,0)f (8,0)f(9,0), s=1
f(0,1)f(1,1)f(2,1)f(3,1)f(4,1)f(5,1)f(6,1)f(7,1)f (8,1)f(9,1), s=1
f(0,2)f(1,2)f(2,2)f(3,2)f(4,2)f(5,2)f(6,2)f(7,2)f (8,2)f(9,2), s=1
f(0,3)f(1,3)f(2,3)f(3,3)f(4,3)f(5,3)f(6,3)f(7,3)f (8,3)f(9,3), s=0
-----------------

Case 2) Line (2) is commented out, (1) is not. The for loop gets
executed only once when f() returns true.
-----------------
f(0,0), s=1
f(0,1), s=1
f(0,2), s=1
f(0,3)f(1,3)f(2,3)f(3,3)f(4,3)f(5,3)f(6,3)f(7,3)f (8,3)f(9,3), s=0
-----------------


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #2
hall wrote:
//s = s||f(x,count); //(1)
bool tmp =f(x, count); s=s||tmp; //(2) Can anyone explain what is going on here and why the two lines don't
behave the same way?


The || operator won't evaluate the right side if the left side is true.
After the first time that f() returns true f() won't be called again
for (1). The code for (2) always calls f().

Jul 22 '05 #3
"hall" <Xc***********@yahoo.se> wrote in message
news:c5**********@eol.dd.chalmers.se...
Hi.

I've come across someting strange. I was trying to make a for-loop
execute repetadly until the function called inside it does not return
true during the entire loop (see program below).

The two lines that confuse me are marked as (1) and (2).

count=0;
bool s(true);
while(s){
s=false;
for (int x=0; x<10; ++x){
//s = s||f(x,count); //(1)

bool tmp =f(x, count); s=s||tmp; //(2)

s=s||tmp means that if s is true it gets reassigned the true value else if
tmp is true s becomes true else if noone is true it is reassigned the false
value since s||tmp evaluates to false.
}
count ++;
}
where f(x,count) is a bool function, returning true for count<3

I thought that they both do the same, ie fun() is executed and bool
variable s = s OR (return from function). However, line (1) will abort
the execution of the for loop already when x=0,

I can't see how line (1) can abort the for loop unless an exception is
thrown or a signal is raised. If you mean that the function call is not
executed 10 times, it happens when after f(x,count) returns true and s gets
that value, at the next evaluations, s evaluates to true so the second
expression is not checked (and thus your functions is not executed at all).
If you want your function to be run 10 times you can make it:

for (int x=0; x<10; ++x){
s = f(x,count) || s;


Ioannis Vranos

Jul 22 '05 #4
On 2004-04-14 16:40 Leor Zolman spoke thusly


Just from what you've said, it seems pretty clear you're not familiar with
the "short circuit" behavior of the || and && operators. In both cases, if
evaluation of the left-hand operand results in a value that "forces" the
logical result of the entire expression, then the right-hand operand is
guaranteed to NOT be evaluated. Otherwise, the right-hand operand is
evaluated and its value is the result of the binary expression.


Thank you. That was exactly what I needed to hear. Now I understand why
things didn't work the way I thought it would.

And thanks to you others who replied too

regards
hall

--
( - Remove capital X from email to reply - )

Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Narya | last post by:
Hello guys, I got a strange problem when using for loop. For example, the following code for ($j = 0.0; $j <= 2; $j+=0.2) { print $j, " "; } gives 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6...
0
by: Michele Laghi | last post by:
Hi, I am faced to the following problem: .... conn.setAutoCommit(false); String val = new String {"one", "two", "two", "three" }; for (int i=0; i < nmax; i++) { try { PreparedStatement pst =...
36
by: Rolloffle | last post by:
A short time ago my fiancée Kimmy found out that she had gotten pregnant. We had a long, hard talk about what to do, if anything. I was in favour of her getting an abortion, though she was...
2
by: Alex | last post by:
Compiler - Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland Linker - Turbo Incremental Link 5.65 Copyright (c) 1997-2002 Borland Platform - Win32 (XP) Quite by accident I stumbled...
5
by: cody | last post by:
I have a very funny/strange effect here. if I let the delegate do "return prop.GetGetMethod().Invoke(info.AudioHeader, null);" then I get wrong results, that is, a wrong method is called and I...
20
by: SpreadTooThin | last post by:
I have a list and I need to do a custom sort on it... for example: a = #Although not necessarily in order def cmp(i,j): #to be defined in this thread. a.sort(cmp) print a
4
by: | last post by:
Im getting that error (It is strange. I Run my programme step by step pressing f11. im looping SqlCommand in a while loop. The First step runs but when a enter 2nd step in my loop it returns error...
3
by: Rinaldo | last post by:
Hi, I have a label on my dialogbox who has to change text while running. This is what I do: lblBackup.Text = "Bezig met de backup naar " + F1.FTPserver; but the text does'nt appear, only if...
6
by: markus.litz | last post by:
Hello, I have a strange problem with some of my c++ code. I have a normal for- loop like this for(int i = 1; ..... on MS-Windows with a Microsoft compiler everything is alright, but when I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.