Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- When using the logical operators, C++ only evaluates what is necessary from left to right to come up with the combined relational result, ignoring the rest. Therefore, in this last example ((5==5)||(3>6)), C++ would evaluate first whether 5==5 is true, and if so, it would never check whether 3>6 is true or not. This is known as short-circuit evaluation, and works like this for these operators:
- operator short-circuit
- && if the left-hand side expression is false, the combined result is false (right-hand side expression not evaluated).
- || if the left-hand side expression is true, the combined result is true (right-hand side expression not evaluated).
- This is mostly important when the right-hand expression has side effects, such as altering values:
- if ((i<10)&&(++i<n)) { /*...*/ }
- This combined conditional expression increases i by one, but only if the condition on the left of && is true, since otherwise the right-hand expression (++i<n) is never evaluated.
Advertisement
Add Comment
Please, Sign In to add comment