Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java Program to use Conditional Operator

In this tutorial, we will learn how to perform conditional operations. The conditional operator consists of three operands and is used to evaluate Boolean expressions. The goal of this operator is to decide; which value should be assigned to the variable. It is also known as a ternary operator. But before moving further, if you are not familiar with the concept of the conditional operator in java, then do check the article on Operators in Java.

Input:

Enter the first number: 4

Enter the second number: 4

String output = (4==4)?"Equal":"Not Equal"

Output: Equal

Two cases arise for the above problem:

Case 1: When values are user-defined

Case 2: When values are pre-defined

Let us look at each of these cases separately.

Program 1: To Perform Conditional Operations

In this program, we will see how to perform Conditional AND and Conditional OR operations when the values are user-defined. Here, we will first ask the user to enter the values and then we will perform the Conditional AND and Conditional OR operation.

Algorithm:

  1. Start
  2. Create an instance of the Scanner class.
  3. Declare three variables.
  4. Ask the user to initialize the variables.
  5. Perform Conditional AND operation and Conditional OR operation.
  6. Display the result.
  7. Stop.

The below example illustrates the implementation of the above algorithm.

//Java Program to perform Conditional AND and OR operations
import java.util.*;
public class Main
{  
    public static void main(String args[])
    {    
        //Take input from the user
        //Create instance of the Scanner class
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter the first number: ");
        int x=sc.nextInt();   //Declare and Initialize the number
        System.out.print("Enter the second number: ");
        int y=sc.nextInt();   //Declare and Initialize the number
        System.out.print("Enter the third number: ");
        int z=sc.nextInt();   //Declare and Initialize the number
        //Conditional AND Operator
        //Conditional OR Operator
        System.out.println("Result of : "+x+">"+y+" && "+x+">"+z+" || "+y+"<"+z);  
        System.out.println(x>y && x>z || y<z);  
        System.out.println("Result of ("+x+"<"+z+" || "+y+">"+z+") && "+x+"<"+y);  
        System.out.println((x<z || y>z) && x<y); 

    }  
}  


Enter the first number: 12
Enter the second number: 11
Enter the third number: 10
Result of : 12>11 && 12>10 || 11<10
true
Result of (12<10 || 11>10) && 12<11
false

Program 2: To Perform Conditional Operations

In this program, we will see how to perform Conditional AND and Conditional OR operations when the values are pre-defined in the program.

Algorithm:

  1. Start
  2. Declare three variables.
  3. Initialize these variables.
  4. Perform Conditional AND operation and Conditional OR operation.
  5. Display the result.
  6. Stop.

The below example illustrates the implementation of the above algorithm.

//Java Program to perform Conditional AND and OR operations
import java.util.*;
public class Main
{  
    public static void main(String args[])
    {    
        int x= 9,y=8,z=6;
        //Conditional AND Operator
        //Conditional OR Operator
        System.out.println("Result of : "+x+">"+y+" && "+x+">"+z+" || "+y+"<"+z);  
        System.out.println(x>y && x>z || y<z);  
        System.out.println("Result of ("+x+"<"+z+" || "+y+">"+z+") && "+x+"<"+y);  
        System.out.println((x<z || y>z) && x<y); 
    }  
}  


Result of : 9>8 && 9>6 || 8<6
true
Result of (9<6 || 8>6) && 9<8
false

Program 3: To Perform Conditional Operations

In this program, we will see how to perform a ternary operation when the values are user-defined. Here, we will first ask the user to enter the values and then we will check the specified condition using the ternary operator and will display the first expression if the condition is true and the second expression if the condition is false.

Algorithm:

  1. Start
  2. Create an instance of the Scanner class.
  3. Declare two variables.
  4. Ask the user to initialize the variables.
  5. Check the condition using a ternary operator.
  6. Display the result.
  7. Stop.

The below example illustrates the implementation of the above algorithm.

//Java Program to perform ternary 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 sc=new Scanner(System.in);
        System.out.print("Enter the first number: ");
        int a=sc.nextInt();   //Declare and Initialize the number
        System.out.print("Enter the second number: ");
        int b=sc.nextInt();   //Declare and Initialize the number
        String out = a==b ? "Yes":"No";
        System.out.println("Is "+a+" == "+b+ "?");
        System.out.println(out);
    }  
}  


Enter the first number: 12
Enter the second number: 11
Is 12 == 11?
No

Program 4: To Perform Conditional Operations

In this program, we will see how to perform the ternary operation when the values are pre-defined in the program.

Algorithm:

  1. Start
  2. Declare two variables.
  3. Initialize the variables.
  4. Check the condition using a ternary operator.
  5. Display the result.
  6. Stop.

The below example illustrates the implementation of the above algorithm.

//Java Program to perform ternary operator

public class Main
{  
    public static void main(String args[])
    {    
        int a=9,b=8;
        System.out.print("The entered number is: "+a);
        System.out.print("The entered number is: "+b);
        String out = a>=b ? "Yes both the numbers are the same":"No both the numbers are not the same";
        System.out.println("Is "+a+" >= "+b+ "?");
        System.out.println(out);
    }  
}  


The entered number is: 9
The entered number is: 8
Is 9 >= 8?
Yes both the numbers are the same