The logical short-circuit OR operator ( ||) is used in the form
operand1 || operand2
The logical short-circuit OR operator returns true if either operand is true.
If both operands are false, it returns false.
If operand1 evaluates to true, it returns true without evaluating operand2. This is why it is called a short-circuit OR operator.
The following code shows how to Logical Short-Circuit OR Operator.
int i = 10; int j = 15; boolean b = (i > 5 || j > 10); // Assigns true to b
public class Main { public static void main(String[] args) { int i = 10; //from ww w . jav a 2 s . com int j = 15; boolean b = (i > 5 || j > 10); // Assigns true to b System.out.println ("b = " + b); } }
Consider another example.
int i = 10; int j = 15; boolean b = (i > 20 || j > 10); // Assigns true to b
The expression i > 20 returns false. The expression reduces to false || j > 10.
Because the left-hand operand to || is false, the right-hand operand, j > 10, is evaluated, which returns true and the entire expression returns true.
public class Main { public static void main(String[] args) { int i = 10; /*from www . j a v a 2 s. c o m*/ int j = 15; boolean b = (i > 20 || j > 10); // Assigns true to b System.out.println ("b = " + b); } }