or execution priority

Feb 10, 2011 at 1:58pm
When a logical OR of two boolean variables is checked, is the second value going to be checked, if the first is true? In the following example, is b going to be checked?

bool a = true
bool b = false

if(a || b) cout << "true" << endl;

Feb 10, 2011 at 2:07pm
No. It's called short circuit evaluation. It works the same for &&.
Feb 10, 2011 at 2:09pm
Thanks :)
Feb 10, 2011 at 2:29pm
If you don't like that behaviour you could create a Boolean class that overloads the operators or and not and provides a cast to bool.
Topic archived. No new replies allowed.