Short Circuit Evaluation

Both && and || use short circuit evaluation. That means that the second condition isn’t necessarily checked. If the code uses && and the first condition is false, the second condition won’t be executed since the result will already be false. If the code uses || and the first condition is true then the second condition won’t be executed since only one of the conditions needs to be true.

Check your understanding

5-3-1: What is printed when the following code executes and x has been set to zero?

if (x == 0 || (y / x) == 3) System.out.println("first case");
else System.out.println("second case");