| by Arround The Web | No comments

How to use OR Statement in Java

In Java, Operators are unique symbols with a pre-defined purpose. They are utilized to perform a specific operation with one or more operands and then return an output. There are many Java operators, such as Arithmetic, Logical, and Bitwise operators. More specifically, the “OR” operator belongs to the category of logical operators. This boolean operator returns “true” if either or both operand’s values are “true”; otherwise, it returns “false”.

This write-up will illustrate the method related to using the OR statement in Java.

How to Use OR Statement in Java?

OR is a logical operator that is utilized in conditional statements like if-else. It is denoted by the symbol “||”. This operator follows the below-given pattern:

  • If one of the conditions is true, it returns true.
  • If both of the conditions are false, it returns false.
  • If both conditions are satisfied, it returns true.

However, in the case of multiple conditions with the OR operation, if the first condition is evaluated as “true”, the OR operator will not verify the second condition either if it is true or false. If the first condition is evaluated as false, it will check the second one.

Syntax
The syntax of the OR statement is given as:

x || y

Or you can compare one or more operands with each other using OR operator:

x || y || z || w

It will return true if any of the conditions are true.

Truth Table of OR Operator

Let’s check out the truth table of the OR operator:

x y x||y
true true true
true false true
false true true
false false false

As the truth table shows that if one of the variable values is “true”, the OR operator will return “true”. In the other case, it returns “false” when both the values are “false”.

Head towards the given examples to know more about the working of the OR statement in Java.

Example 1
First, we will create two integer type variables, “a” and “b”, and assign them the following values:

int a = 11;
int b = 5;

Then, create another int type variable “s” that will store the sum of the above-created variables:

int s;

Now, in the “if” statement, we will check whether the value of the variable “a” is greater than “b” by using OR operator. If any of the conditions is true, add the values of the specified variable and store their sum in “s”:

if(a>b || b<a) {
    s = a+b;
      System.out.println("The sum is " +s);
}

The compiler will check the first condition that is “a>b” if it is evaluated as true, then the next added condition will be ignored, and the body of the if statement will be executed:

As the value of the “a” variable is greater than “b”, their sum will be displayed as output:

Let’s move to the next example to see how the OR operator checks other types of conditions.

Example 2
Here, we have two character type variables, “c” and “c1”, with the same values and different cases:

char c ='F';
char c1 = 'f';

Then, we will utilize the OR operator “||” to check the below-given conditions:

if(c==c1 || c=='F') {
    System.out.println("The value stored in c is "+ c);
}

Here, the first condition “c==c1” will be evaluated as “false” because we know that the same uppercase letter and lowercase letter are not equal. So, the OR operator will check the next statement that is true:

Output

Next, we will see how more than two conditions are checked using OR statement. So, let’s go!

Example 3
First, we will create four integer type variables, “a”, “b”, “c”, and “d”, and assign them the following values:

int a = 11;
int b = 5;
int c = 1;
int d = 12;

After doing so, we will utilize the OR operator to test some conditions based on the values of the created variables:

if(a>d || a>c || a>b) {
    System.out.println("'a' is greater than b and c");
}

The OR operator will evaluate the first condition “a>d” as false and it checks the next condition “a>c” which is true. As a result, the third condition will not be executed:

The program will print out the added statement after evaluating the second condition as true:

What does the OR operator do when all of the conditions are evaluated as false? The following example will answer your question!

Example 4
In this example, we have three integer type variables with the following values:

int b = 8;
int c = 1;
int d = 6;

Then, we will add an if-else block, where the if statement comprises the conditions that the OR operator will evaluate as false.

if(c>d || c>b) {
    System.out.println("'c' is greater than b and d");
}
else
    System.out.println("'c' is less than b and d");
}

As both statements are not true, else block will be executed, and it will display the following statement on the console:

We have provided all of the essential information related to using the OR statement in Java.

Conclusion

OR is a logical operator in Java that is mostly utilized in control statements such as if-else. It outputs true if any one of the statements is evaluated as true; otherwise, it will be returned as false. OR operator checks the condition from the left side. It checks the first condition; if it is true, it will not verify the other conditions and move to the body of the if statement. This write-up illustrates how to use OR statements in Java.

Share Button

Source: linuxhint.com

Leave a Reply