Short-circuit AND Operator




A good answer might be:

12 < 6 && 18 > 1 evaluates to false

AND Operator &&
true  && true   true
false && true   false
true  && false   false
false && false  false

Short-circuit AND Operator

You may have noticed something in answering the
question:
you can get the correct answer to the question by
evaluating just the first part of the expression:

12 < 6 && 18 > 1
------ 
false

Since false && anything is false,
there is no need
to continue after the
first false has been encountered.
In fact,
this is how Java operates:

To evaluate X && Y, first evaluate X.
If X is false then stop: the whole expression
is false.
Otherwise, evaluate Y then AND the two values.

This idea is called short-circuit evaluation.
Programmers frequently make use of this feature.
For example,
say that two methods that return true/false values
are combined in a boolean expression:

if ( methodThatTakesHoursToRun() && methodThatWorksInstantly() )
   ....

QUESTION 2:


Suggest a better (but logically identical) way to arrange this
boolean expression.