Evaluation order in an 'if' statement
-
Hi all, In an if statemen
if ((Comp1) && (Comp2))
isComp1
evaluated first andComp2
after? I mean, ifComp1
changes a variable, will this variable be changed by the timeComp2
is evaluated? Hence, can I use a variable assigned in the first comparisson to perform the second? Or would I have to do two if's in a row?if(Comp1)
if(Comp2)Thank you!
-
Hi all, In an if statemen
if ((Comp1) && (Comp2))
isComp1
evaluated first andComp2
after? I mean, ifComp1
changes a variable, will this variable be changed by the timeComp2
is evaluated? Hence, can I use a variable assigned in the first comparisson to perform the second? Or would I have to do two if's in a row?if(Comp1)
if(Comp2)Thank you!
Yes, Comp1 is evaluated first. And also, if Comp1 is false, then Comp2 won't be evaluated. So you the variable will be modified once you check the second comparison.
Cédric Moonen Software developer
Charting control [v1.4] -
Hi all, In an if statemen
if ((Comp1) && (Comp2))
isComp1
evaluated first andComp2
after? I mean, ifComp1
changes a variable, will this variable be changed by the timeComp2
is evaluated? Hence, can I use a variable assigned in the first comparisson to perform the second? Or would I have to do two if's in a row?if(Comp1)
if(Comp2)Thank you!
yes, comp1 is avaluated before comp2. also, you have to know that depending on the comparison operator used, the other "comps" may not be evaluated. check this :
if ((1==1) || (c == d)) ...
here, the test (c == d) is never performed, because (1 == 1) is true, so
true **OR** anything
is actually true. same is this :if ((0==1) && (c == d)) ...
same is (0 == 1) always returns false, so
false **AND** anything
is false... so if that even happens, when you have a function call of a variable modification in such a construction, it may not be performed... basically, a if/while conditional statement should be used only for testing a condition to avoid mistakes.[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
Hi all, In an if statemen
if ((Comp1) && (Comp2))
isComp1
evaluated first andComp2
after? I mean, ifComp1
changes a variable, will this variable be changed by the timeComp2
is evaluated? Hence, can I use a variable assigned in the first comparisson to perform the second? Or would I have to do two if's in a row?if(Comp1)
if(Comp2)Thank you!
piul wrote:
In an if statemen if ((Comp1) && (Comp2)) is Comp1 evaluated first and Comp2 after?
It's called short-circuit evaluation.
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Hi all, In an if statemen
if ((Comp1) && (Comp2))
isComp1
evaluated first andComp2
after? I mean, ifComp1
changes a variable, will this variable be changed by the timeComp2
is evaluated? Hence, can I use a variable assigned in the first comparisson to perform the second? Or would I have to do two if's in a row?if(Comp1)
if(Comp2)Thank you!
Also, if you're not entirely clear on operator precedence and ssociativity in C++, then maybe keep a link to this handy: Operator Precedence and Associativity[^] Mark
Mark Salsbery Microsoft MVP - Visual C++ :java: