PUBLISHED ON: MAY 4, 2021
Java Program to Perform Logical Operations
In this tutorial, we will learn how to perform logical operations by taking input from the user. Logical operators are used for checking whether an expression is true or false. They are used in decision-making. The logical operators present in java are &&, ||, and !. But before moving further, if you are not familiar with the concept of the logical operator in java, then do check the article on Operators in Java.
Input: (5 > 3 ) && (8 > 5 )
(5 < 3 ) || (8 > 5 )
!(5==5)
Output:
true
true
false
Program 1: To Perform Logical Operation
In this program, we will see how to perform logical operations in java when the values are user-defined. Here, firstly we will ask the user to input the values, and then we will perform the logical operations.
Algorithm:
- Start
- Here, we will use a switch case to choose from different logical operations like &&, || and !
- Declare a variable for the same.
- Ask the user to initialize it.
- Based on the operation chosen, declare two variables.
- Ask the user to initialize the variables.
- Display the result after performing the logical operations.
- Stop.
Let us look at the below example for a better understanding of the above algorithm.
//Java Program to perform logical operation
import java.util.*;
public class Main
{
public static void main(String[] args)
{
//Take input from the user
//Create instance of the Scanner Class
Scanner s=new Scanner(System.in);
while(true)
{
System.out.println("");
System.out.println("Choose the operation you want to perform ");
System.out.println("Choose 1 for && ");
System.out.println("Choose 2 for || ");
System.out.println("Choose 3 for ! ");
System.out.println("Choose 4 for EXIT");
int n = s.nextInt();
switch(n)
{
case 1:
System.out.println("Enter the two numbers to perform operations ");
System.out.print("Enter the first number : ");
int x = s.nextInt();
System.out.print("Enter the second number : ");
int y = s.nextInt();
System.out.print("Enter the third number : ");
int z = s.nextInt();
System.out.println("Result of ("+x+" > "+y+") && ("+z+" > "+x+")");
System.out.println((x > y) && (z > x));
System.out.println("Result of ("+x+" > "+y+") && ("+z+" < "+x+")");
System.out.println((x > y) && (z < x));
break;
case 2:
System.out.println("Enter the two numbers to perform operations ");
System.out.print("Enter the first number : ");
int p = s.nextInt();
System.out.print("Enter the second number : ");
int q = s.nextInt();
System.out.print("Enter the third number : ");
int r = s.nextInt();
System.out.println("Result of ("+q+" < "+p+") || ("+r+" > "+p+")");
System.out.println((q < p) || (r > q));
System.out.println("Result of ("+q+" > "+p+") || ("+r+" < "+p+")");
System.out.println((q > p) || (r < q));
System.out.println("Result of ("+q+" < "+p+") || ("+r+" < "+p+")");
System.out.println((q < p) || (r < q));
break;
case 3:
System.out.println("Enter the two numbers to perform operations ");
System.out.print("Enter the first number : ");
int a = s.nextInt();
System.out.print("Enter the second number : ");
int b = s.nextInt();
System.out.println("Result of (!"+a+" == "+b+")");
System.out.println(!(a == b));
System.out.println("Result of (!"+a+" > "+b+")");
System.out.println(!(a > b));
break;
case 4:
System.exit(0);
}
}
}
}
Choose the operation you want to perform
Choose 1 for &&
Choose 2 for ||
Choose 3 for !
Choose 4 for EXIT
1
Enter the two numbers to perform operations
Enter the first number : 1
Enter the second number : 2
Enter the third number : 3
Result of (2 > 3) && (4 > 2)
false
Result of (2 > 3) && (4 < 2)
false
Choose the operation you want to perform
Choose 1 for &&
Choose 2 for ||
Choose 3 for !
Choose 4 for EXIT
2
Enter the two numbers to perform operations
Enter the first number : 5
Enter the second number : 6
Enter the third number :7
Result of (6 < 5) || (7 > 5)
true
Result of (6 > 5) || (7 < 5)
true
Result of (6 < 5) || (7 < 5)
false
Choose the operation you want to perform
Choose 1 for &&
Choose 2 for ||
Choose 3 for !
Choose 4 for EXIT
3
Enter the two numbers to perform operations
Enter the first number:8
Enter the second number: 9
Result of (!8 == 9)
true
Result of (!8 > 9)
true
Choose the operation you want to perform
Choose 1 for &&
Choose 2 for ||
Choose 3 for !
Choose 4 for EXIT
4
Program 2: To Perform Logical Operation
In this program, we will see how to perform logical operations in java when the values are pre-defined.
Algorithm:
- Start
- Declare three variables.
- Ask the user to initialize the variables.
- Declare variables to store the result after performing logical operations.
- Display the result after performing all the logical operations.
- Stop.
Let us look at the below example for a better understanding of the above algorithm.
//Java Program to perform logical operation
import java.util.*;
public class Main
{
public static void main(String[] args)
{
int x=2,y=3,z=4;
// && operator
System.out.println("Result of ("+x+" > "+y+") && ("+z+" > "+x+")");
System.out.println((x > y) && (z > x));
System.out.println("Result of ("+x+" > "+y+") && ("+z+" < "+x+")");
System.out.println((x > y) && (z < x));
// || operator
System.out.println("Result of ("+y+" < "+x+") || ("+z+" > "+x+")");
System.out.println((y < x) || (z > y));
System.out.println("Result of ("+y+" > "+x+") || ("+z+" < "+x+")");
System.out.println((y > x) || (z < x));
System.out.println("Result of ("+y+" < "+x+") || ("+z+" < "+x+")");
System.out.println((y < x) || (z < y));
// ! operator
System.out.println("Result of (!"+x+" == "+y+")");
System.out.println(!(x == y));
System.out.println("Result of (!"+x+" > "+y+")");
System.out.println(!(x > y));
}
}
Result of (2 > 3) && (4 > 2)
false
Result of (2 > 3) && (4 < 2)
false
Result of (3 < 2) || (4 > 2)
true
Result of (3 > 2) || (4 < 2)
true
Result of (3 < 2) || (4 < 2)
false
Result of (!2 == 3)
true
Result of (!2 > 3)
true