Java Program to Check if two of three boolean variables are true

To understand this example, you should have the knowledge of the following Java programming topics:


Example: Check if two of the three boolean variables are true

Output 1

Enter first boolean value: true
Enter second boolean value: false
Enter third boolean value: true
Two boolean variables are true.

Output 2

Enter first boolean value: false
Enter second boolean value: true
Enter third boolean value: false
Two boolean variables are not true.

In the above example, we have three boolean variables named first, second, and third. Here, we have checked if two of the boolean variables among the three are true or not.

We have used the if...else statement to check if two boolean variables are true or not.

if(first) {
  result = second || third;
}
else {
  result = second && third;
}

Here, instead of the if...else statement, we can also use the ternary operator.

result = first ? second || third : second && third;