Here's the code.
#include <iostream>
using namespace std;
int a = 0;
int foo()
{
a++;
return a;
}
int main()
{
if(1 || foo())
{
cout << "Hello World\n";
}
cout << a << endl;
return 0;
}
Result : 0 is displayed.
Change line 15 to this:
if(foo() || 1)
Result : 1 is displayed.
"Hello World" is obviously displayed in both cases. The "if" statement can only evaluate to true, regardless of order, so why bother calling foo() in the second instance? It was smart enough not to in the first instance. And does C++ REQUIRE foo() to be called in the second instance?