next up previous
Next: Eliminating the disjunction operator Up: Unit 05 Previous: Shortcut evaluation of a

Eliminating the conjunction operator && in a complex condition

The code fragment

if ((x < y) && (y < z))
  System.out.println("y is between x and z");
else
  System.out.println("y is not between x and z");
corresponds to
if (x < y)
  if (y < z)
    System.out.println("y is between x and z");
  else
    System.out.println("y is not between x and z");
else
  System.out.println("y is not between x and z");

In this case, by eliminating the complex condition, the code of the else-branch must be duplicated.

Note that, due to shortcut evaluation, the second condition in the && is not evaluated if the first condition is false. And this holds also for the corresponding nested if-else statements.


next up previous
Next: Eliminating the disjunction operator Up: Unit 05 Previous: Shortcut evaluation of a