Multiple OR or AND conditions in IF statement
Solution 1
According to the C++ Standard
1 The && operator groups left-to-right. The operands are both contextually converted to bool (Clause 4). The result is true if both operands are true and false otherwise. Unlike &, && guarantees left-to-right evaluation: the second operand is not evaluated if the first operand is false.
and
1 The || operator groups left-to-right. The operands are both contextually converted to bool (Clause 4). It returns true if either of its operands is true, and false otherwise. Unlike |, || guarantees left-to-right evaluation; moreover, the second operand is not evaluated if the first operand evaluates to true.
Solution 2
It returns "true" on the first mismatch. Operators in both C and C++ "short-circuit". That is, an OR operator will not evaluate its right side when its left side returned true. The AND operator will not evaluate its right side if its left side returned false.
user3663688
Updated on July 09, 2022Comments
-
user3663688 almost 2 years
I am having a basic doubt regarding IF statement. Let's say I want to match string SUN with a character array(size 3).
if(arr[0]!='S' || arr[1]!='U' || arr[2]!='N') cout << "no"; else cout<< "yes";
Are all conditions checked in If statement or does it return true on first mismatch?
If all conditions are checked, will the the order of checking be from right to left?
-
Tim almost 10 yearsAre you using c or c++?
-
Leeor almost 10 yearsNote that it's not really a c-style string (no null termination)
-
Lundin almost 10 yearsThis is C++ not C. Edited the post to fix this.
-
CodaFi almost 10 yearsC and C++ don't "evaluate" any particular direction, they short circuit in cases like this.
-
Kevin Cook almost 10 yearsthis is c++, the conditions will all be checked until the first one fails the condition, and then it will fall out. (I.E. it short circuits on the first evaluation that will fall out)
-
Lundin almost 10 years@CodaFi They do evaluate in one given, particular direction, left-to-right. And there is a sequence point in between every evaluation. The compiler may stop further evaluation of the || as soon as it has encountered an operand which evaluates to true.
-
Sergey Kalinichenko almost 10 yearsA chain of
||
and&&
is evaluated left-to-right. If the result can be confirmed before reaching the end of the expression chain, the computation is terminated early. -
CodaFi almost 10 years@Lundin Perhaps I should clarify: "Evaluate" has strong implications, at least to me, of interpreted languages. Binaries are not "evaluated" at runtime. And what you've just described is what I just linked to.
-
dhein almost 10 yearsYou should check this article, it is pretty awesome explained ;) stackoverflow.com/q/23940505/2003898
-
Lundin almost 10 years@CodaFi Evaluation in this context, refers to order of evaluation (of operands to a certain operator), which is a formal term in the C and C++ standards.
-
CodaFi almost 10 years@Lundin Let's hope so, 'cause that code's looking a little suspect. I can edit and repost the comment if you'd like.
-