salamanderrake

wtf++

May 23rd, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. 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:
  2.  
  3. operator short-circuit
  4. && if the left-hand side expression is false, the combined result is false (right-hand side expression not evaluated).
  5. || if the left-hand side expression is true, the combined result is true (right-hand side expression not evaluated).
  6.  
  7. This is mostly important when the right-hand expression has side effects, such as altering values:
  8.  
  9.  
  10. if ((i<10)&&(++i<n)) { /*...*/ }
  11.  
  12.  
  13. 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