# Java Logical Operators

The Java Logical operators combine two or more conditions and perform the logical operations using && (AND), || (OR) and ! (NOT).

The Java Relational Operators compare two variables and, what if we want to compare more than one condition? Very simple, Java logical operators will do the trick. The table below describes the Java Logical Operators with examples

Let us see the truth tables behind the logical operators in Java for better understanding

&& (Java LOGICAL AND) Truth table

|| (Java LOGICAL OR) Truth table

## Java Logical Operators example

This example will help to know how logical operators in Java Programming used in If statements. For this example, we are using one integer variable. Inside the If Statement, we used relational and logical operators in Java to perform condition check.

```// Using Logical Operators in Else If Statement

package JavaOperators;

import java.util.Scanner;

public class LogicalOperators {
private static Scanner sc;

public static void main(String[] args) {
int age;
sc = new Scanner(System.in);
age = sc.nextInt();

if (!(age > 18)) {
System.out.println("You are Too Young");
}
else if (age > 18 && age <= 35) {
System.out.println("Young Man");
}
else if (age == 36 || age <= 60) {
System.out.println("You are Middle Age Person");
}
else {
System.out.println("You are too Old");
}
}
}```

From the Java screenshot below, see that we entered age = 12. It means age is not greater than 18. So, the first statement printed.

Let us see what will happen when we change our values as age = 28. It means age is between 18 and 35. So, the second statement printed.

`````` Please Enter Your Age:
28
Young Man``````

This time,  we entered age= 56. It means age is between 36 and 60. So, the third statement printed.

`````` Please Enter Your Age:
56
You are Middle Age Person``````

We entered age= 65. So, it prints the message in else block

`````` Please Enter Your Age:
65
You are too Old``````

In this example, the first statement will ask the user to enter his / her age. Next, we are going to assign the user input value to a variable.

The following Java Logical Operators inside the If statement will check whether age is not greater than 18 (technically, age is less than 18 or not). If the condition is True, statement inside the If block executed

The following else If condition will check whether age is greater than 18 and age is less than or equal to 35. If both these conditions are True, statement inside the Else If block executed.

The next else if condition in Java Logical Operators example check whether age is equal to 36 and age is less than or equal to 60. If both these conditions are True, statement inside the Else If block executed.

If everything fails, then the statement inside the else block “You are too Old” executed.