3

Consider this code:

int a = 5;
if (a == 5 || a == 10)
  doSomething();

In this case a is 5 so the first condition is true. Will the program check if the second condition is true or it will start executing doSomething() immediately after it makes sure that a is really 5?

razlebe
  • 7,134
  • 6
  • 42
  • 57
Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161
  • 1
    No, it won't check the second condition - it will only go as far through the conditions as necessary. If it's evident what the truth value of the condition is, partway through, it won't bother with the other rest of the condition. Possible duplicate of [How does C++ handle &&? (Short-circuit evaluation)](http://stackoverflow.com/questions/5211961/how-does-c-handle-short-circuit-evaluation) : different operator, same issue. See also: http://en.wikipedia.org/wiki/Short-circuit_evaluation – Piskvor left the building Feb 03 '12 at 16:30
  • check the assembly output, and you would find out. use the -S to the compiler, some may be different. – L7ColWinters Feb 03 '12 at 16:31
  • 1
    @Piskvor Not quite a duplicate - this one is tagged "C" rather than "C++" (even though in the case of the || operator they have common behaviour) – razlebe Feb 03 '12 at 16:33
  • @razlebe: And it's `&&` and not `||`, I know. C != C++, but in this regard they behave the same way. – Piskvor left the building Feb 03 '12 at 16:34
  • `a==10` will not be checked in your case. – Ortwin Angermeier Feb 03 '12 at 16:35
  • possible duplicate of [Is short-circuiting boolean operators mandated in C/C++? And evaluation order?](http://stackoverflow.com/questions/628526/is-short-circuiting-boolean-operators-mandated-in-c-c-and-evaluation-order) – Wooble Feb 03 '12 at 16:37

5 Answers5

10

It will start executing immediately. This is known as short-circuit evaluation.

http://en.wikipedia.org/wiki/Short-circuit_evaluation

japreiss
  • 11,111
  • 2
  • 40
  • 77
4

The second condition won't be checked. C short-circuits logical evaluations, so as soon as the truth or falsehood of the condition can be determined it stops.

Note that given the code as posted, the compiler might not even generate code to perform the first comparison as it can determine at compile time that the condition is satisfied and no intervening code could alter the value of a.

Share and enjoy.

1
int a = 5;
if (a == 5 || a == 10)
    doSomething();

in this example the left operand operand of || is evaluated to 1 so the right operand will not evaluated.

The compiler is required to not evaluate the right operand of || operator when the left operand is evaluated to 1.

ouah
  • 142,963
  • 15
  • 272
  • 331
1

When it comes to the logical operations, the compiler will stop evaluating the expression as soon as its finds the truth or falsehood of the expression.

The truth-table for logical or (||) operation is as follows:

A   B   A||B  A&&B
0   0    0     0
0   1    1     0
1   0    1     0
1   1    1     1

So for an expression like a == 5 || a == 10, when a is equal to 5, the expression will be evaluated to true when the compiler sees a == 5 part. So irrespective of whether the rest of the expression is evaluated to true or false, due to the logical or (||) operator (refer the above truth-table), this expression will be evaluated to true. So the compiler will discard executing the rest of the expression.

Sangeeth Saravanaraj
  • 16,027
  • 21
  • 69
  • 98
1

Any decent compiler will not even check the first condition since at compile time it knows at once that it should invoke your method. (but of course all comments about short circuit hold true - but not here ;-)

Artur
  • 7,038
  • 2
  • 25
  • 39