. Advertisement .
..3..
. Advertisement .
..4..
This article will guide you thoroughly with insights regarding Or statement Java. Be ready for a brand new conquer of knowledge on the way to your mind!
What Is Or Statement Java?
Before getting to know about or statement Java, there are some things you must first acquire: In fact, there are three distinct statement kinds in Java. These statements include declaration, expression, and control flow.
In control-flow expressions, we may employ the logical or operator OR to carry out a problem statement in Java.
As such, Java’s OR (||
) logical operator is frequently used in if-else statements to handle multiple situations.
To put it more clearly, if one of the criteria is true, the OR
statement results as true. If the first condition is true, it won’t check to see if the second is true or false. If the former falses, it will then examine the second one.
Example Of Or Statement Java
Let’s check out this Or statement Java example so you can grasp more on its working mechanism!
Running the code:
class Main{
public static void main(String args[]){
int x=20;
int y=10;
System.out.println(x>y||x++<y);//true || false = true
System.out.println(x);//20 because second condition is not checked
}
}
Output:
true
20
In the aforementioned situation, we can behold that the first statement does not false the premise of the condition. Because of this, the second condition will not then be verified. As a consequence, the value of variable x would either change or grow.
Here comes a thing you might not yet know: A block of code can be run by combining the if statement with the OR
operator. Whether a condition is met or not, the if statement runs some code either way. In the if statement, we may compare several criteria using the OR operator.
Have a look at the example below also for further understanding.
Running the code:
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.
One of the requirements is true in the case shown above. As a result, the if block is carried out after the OR
operator returns true.
The Bottom Line
The information provided above should be sufficient for you to be informed about the or statement Java. Make sure you’ve gone through this article completely so you can benefit greatly from these ideas.
Also, don’t be afraid to comment if you have any questions about any problem not yet resolved!
Leave a comment