int b=0;Which works in C++. b=1-b just toggles a true/false flag. Using a bool would be easier to understand.
for (int i=0;i<10;i++)
{
if (b)
cout << "true" << endl;
else
cout << "false" << endl;
b=1-b;
}
What does this example program output?
//With the compilers I've tried (both from Microsoft) the output has been this:
#include <iostream>
using namespace std;
int first(int & value)
{
value = 10;
cout << "First" << endl;
return value;
}
int second(int & value)
{
cout << "Second" << endl;
return value;
}
int main()
{
int value = 20;
cout << (first(value) / second(value)) << '\n';
return 0;
}
FirstBut it is not guaranteed as the order of evaluation is indeterminate. One compiler might order and evaluate the expression this way. Another might evaluate both functions in the reverse order.
Second
1
These functions have side-effects; they modify the passed in parameter so the results are affected by the order. Even changing a compiler's optimize setting might alter the order of evaluation and thus the result. Be on the look out for side-effects and try to avoid them.
On the next page : Operator Precedence in C++

