Skip to main content
GameDev.net gamedev.net
🔒 Locked

[C++] Order of evaluation in an if-statement

Started by Sol Blue Mar 30, 2009 at 8:37 PM 5 replies 10.9k views
Original Post
Sol Blue
Sol Blue
Quick C++ question: when I have a line like
if (clause1 && clause2)
will the program stop evaluating the conditional if clause1 is false? In other words, will it realize it doesn't need to check clause2? What about vice versa, with an OR statement with a true first clause?
xissburg
xissburg
If clause1 is false it'll evaluate that whole expression as false. You could test it.
.
Sneftel
Sneftel
Quote:
Original post by Sol Blue
will the program stop evaluating the conditional if clause1 is false? In other words, will it realize it doesn't need to check clause2? What about vice versa, with an OR statement with a true first clause?
Yes on both counts. This is known as "short-circuting", is guaranteed by the language standard, and is used by a lot of code.
Nypyren
Nypyren
Left to right.

If a nested condition like (A && B) || (C && D) is used, it goes like this:

1. if A is false, goto 3.
2. if B is true, bailout true.
3. if C is false, bailout false.
4. if D is false, bailout false.
SiCrane
SiCrane
Short circuit evaluation will only happen with built in versions of && and ||. If they're overloaded, then both sides will need to be evaluated.
Sol Blue
Sol Blue
Wonderful. Thanks very much, everyone!
samoth
samoth
Quote:
Original post by Nypyren
Left to right.

If a nested condition like (A && B) || (C && D) is used, it goes like this:
Careful, though. Do note the parentheses. :-)
Nypren's explanation to the example is correct, but in fact evaluation does not go left to right. && has precedence over ||, therefore the parentheses are needed.

A similar catch comes with == which has precedence over && and ||.
if(a == b && c == d) means if(a == (b && (c == d))) rather than if((a == b) && (c == d)) which one might wrongly assume. Luckily, every decent compiler will warn about that when warnings are enabled.
visitor
visitor
Quote:

A similar catch comes with == which has precedence over && and ||.
if(a == b && c == d) means if(a == (b && (c == d))) rather than if((a == b) && (c == d)) which one might wrongly assume. Luckily, every decent compiler will warn about that when warnings are enabled.


== having higher precedence means precisely that the brackets are optional and (a == b && c == d) means exactly the same as ((a == b) && (c == d)):

#include <iostream>int main(){    int a = 5, b = 6;    std::cout << std::boolalpha    << (a == 5 && b == 6) << '\n'    << ((a == (5 && (b == 6)))) << '\n'    << ((a == 5) && (b == 6)) << '\n';}


This is similar to math: a * b + c * d. Multiplication has higher precedence than addition, so a * b and c * d have to be done before adding the subresults, except unlike logical operators where evaluation is left-to-right, here the compiler might decide to evaluate c * d first (which won't matter to you unless operands have side effects in which case you might be in undefined behaviour land). Or more than that, if the operands are for example results from function calls, then unlike logical operators, it is unspecified in which order the functions will be called.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.