code_junkie

Benefits of using short-circuit evaluation

Nov 10th, 2011
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. boolean a = false, b = true;
  2. if ( a && b ) { ... };
  3.  
  4. (a) if ( ( a != null ) && ( a.equals(b) ) ) { ... }
  5.  
  6. (b) if ( ( a == 4 ) && ( b == 5 ) )
  7.  
  8. open($filename) or die("couldn't open file");
  9.  
  10. do_something || echo "that failed"
  11.  
  12. if ((obj != 0) && (obj->ready)) { do_something; } // not -> in Java of course.
  13.  
  14. if ( ConfirmAction() && DestroyAllData() )
  15. Reboot();
  16.  
  17. if ( myDodgyVar != null && myDodgyVar.IsActive() )
  18. DoSomethingWith(myDodgyVar);
  19.  
  20. IF A And B THEN
  21. ...
  22. END IF
  23.  
  24. IF A AndAlso B THEN
  25. ...
  26. END IF
  27.  
  28. If MyObj IsNot Nothing AndAlso MyObj.Value < SomeValue Then
  29. ....
  30. End If
  31.  
  32. if (a != null && a.equals(somevalue)) {
  33. ... do something.
  34. }
  35.  
  36. if (someObject.isActive() && someOtherObject.isActive() && CollisionDetection.collides(someObject, someOtherObject) {
  37. doSomething();
  38. }
  39.  
  40. a = obj.somethingQuickToTest() && obj.somethingSlowToTest();
  41.  
  42. a = false;
  43. if(obj.somethingQuickToTest())
  44. a = obj.somethingSlowToTest();
Add Comment
Please, Sign In to add comment