H
hall
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
-----------------
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
-----------------