Java Program on Logical OR Operator

In the previous article, we have discussed about Java Program on Logical AND Operator

In this article we will see the use of Logical OR operator in Java programming language.

Java Program on Logical OR Operator

Logical OR operator is represented by the symbol ||. This operator returns True if any one side be it left or right side expression is true. When both sides condition are false in that case it returns False.

Syntax: Expression1 || Expression2

Where the expression holds a condition and that Expression1, Expression2 are both operands for logical OR operator.

For Example:

Suppose you have 3 integer variables a, b, c. 
a=5, b=8, c=3 

Case-1 

if(a>b || a>c) then add a, b , c   // Here left side condition i.e. a>b does not satisfy. But right side condition i.e. a>c satisfies. So, here OR operator will return True. 

Case-2 

if(b<a && b<c) then add a, b , c   // Here left side condition i.e. b<a does not satisfy. Also right side condition i.e. b<c does not satisfy. So, here OR operator will return False.

Actually in case of OR operator when left hand side condition fails means first condition is False, then it checks the next condition that is in right side. If any condition is True then it returns True.

Program:

class Main
{
    public static void main(String[] args)
    {
        //initializing three integer variables a, b, c
        int a = 5, b = 8, c = 3;
  
        //Printing values of a, b, c
        System.out.println("Value of a = " + a);
        System.out.println("Value of b = " + b);
        System.out.println("Value of c = " + c);
  
        //using logical OR
        //here first condition is false but second condition is true so OR operator will return True
        //so it will go into if block
        if ((a>b) || (a>c)) 
        {
            System.out.println("Result1 = "+(a+b+c));
        }
        //here both conditions are false so OR operator will return False
        //so it will not go into else if block
        else if ((b<a) || (b<c)) 
            System.out.println("Result2 = "+(a+b+c));
    }
}
Output:

Value of a = 5
Value of b = 8
Value of c = 3
Result1 = 16

Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.

Related Java Programs: