Or Statement in Java

Java supports three types of statements. These are expression, declaration, and control-flow statements. OR is a logical or operator which we can use in control-flow statements to execute a problem statement in Java.
OR (||
) is a logical operator in Java that is mainly used in if-else statements when dealing with multiple conditions. The OR statement returns true if one of the conditions is true. If we get a true condition initially, it will not go and check the second condition, whether true or false. It will check the second condition if the first one is false.
For example,
class Main{
public static void main(String args[]){
int x=10;
int y=5;
System.out.println(x>y||x++<y);//true || false = true
System.out.println(x);//10 because second condition is not checked
}
}
Output:
true
10
In the above example, the first condition is true. That is why the second condition is not checked, and the value for variable x remains the same and is not incremented.
The OR operator can be used with the if
statement to execute a block of code. The if
statement executes some code when a condition is true or not. We can use the OR operator to compare multiple conditions in the if
statement.
See the following example.
public class Main{
public static void main(String[] args){
String month="November";
if(month=="November"|| month=="January"){
System.out.println("Month of winter.");
}
}
}
Output:
Month of winter.
In the above example, one of the conditions is true. So the OR operator returns true, and the if block is executed.