Check If Two of Three Boolean Variables Are True in Java



In this article, we will learn to check if any two out of three boolean values are true or not. Boolean variables are datatypes that contain only true or false values.

Below is a demonstration of the same -

Input: true, true, false

Output : Two of the three variables are true

Checking if two of three Boolean variables are true

In this article, we will discuss two approaches to check it, and they are -

  • Using if-else Condition

  • Using the ternary/conditional operator

Using if-else Condition

To learn about the if-else condition, you can refer if-else condition tutorial.

  • Start the program.

  • Initialize three boolean variables: first, second, and third.

  • Declare a boolean variable result to store the result.

  • If the first is true, set the result = second || third.

  • Else, set result = second && third.

  • If the result is true, print "Two boolean variables are true", else print "Two boolean variables are not true".

Example

class Bool {
   public static void main(String[] args) {

      // Assign boolean values 
      boolean first = true;
      boolean second = false;
      boolean third = true;

      boolean result;

      // Check if two variables are true
      if (first) {
         result = second || third;
      } else {
         result = second && third;
      }

      if (result) {
         System.out.println("Two boolean variables are true.");
      } else {
         System.out.println("Two boolean variables are not true.");
      }
   }
}

On compiling, the above program gives you the following output.

Two boolean variables are true.

Example

Here, the input is being entered by the user based on a prompt. You can try this example live in our coding ground tool run button.

import java.util.Scanner;
public class BooleanValues {
   public static void main(String[] args) {
      boolean my_input_1, my_input_2, my_input_3, my_result;
      System.out.println("The required packages have been imported");
      System.out.println("A scanner object has been defined ");
      Scanner my_scanner = new Scanner(System.in);
      System.out.print("Enter the first boolean value: ");
      my_input_1 = my_scanner.nextBoolean();
      System.out.print("Enter the second boolean value: ");
      my_input_2 = my_scanner.nextBoolean();
      System.out.print("Enter the third boolean value: ");
      my_input_3 = my_scanner.nextBoolean();
      if(my_input_1) {
          my_result = my_input_2 || my_input_3;
      } else {
         my_result = my_input_2 && my_input_3;
      }
      if(my_result) {
         System.out.println("Two of the three variables are true");
      } else {
         System.out.println("Two of the three variables are false");
      }
   }
}

On compiling, the above program gives you the following output.

The required packages have been imported
A scanner object has been defined
Enter the first boolean value: true
Enter the second boolean value: true
Enter the third boolean value: false
Two of the three variables are true

Using the Ternary operator

To learn about the ternary operator/conditional operator, you can refer ternary operator tutorial.

  • Start the program.

  • Initialize three boolean variables: first, second, and third with predefined values.

  • Use a ternary operator. If the first is true, set result = second || third, else, set result = second && third.

  • Check if the output is true.

  • If true, print "Two boolean variables are true".

  • Else, print "Two boolean variables are not true".

Example 1

class Bool {
   public static void main(String[] args) {
      // Define boolean values (without Scanner)
      boolean first = true;
      boolean second = false;
      boolean third = true;

      // Use ternary operator to determine result
      boolean result = first ? (second || third) : (second && third);

      // Output the result
      System.out.println(result ?
         "Two boolean variables are true." :
         "Two boolean variables are not true.");
   }
}

On compiling, the above program gives you the following output.

Two boolean variables are true.
Manisha Chand
Manisha Chand

Words That Decode Code

Updated on: 2025-06-05T10:51:50+05:30

439 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements