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.