Updated March 27, 2023
Introduction to Conditional Operator in Java
Conditional Operators in Java are the ones that use three operands, and they are used to work on conditions. It is a unique operator that is used in place of If-Else statements. The Conditional Operator’s main advantage is that it can be completed in only one sentence, whereas the if-else statement uses multiple code lines. However, there is also a disadvantage of the conditional operator: it cannot be used for multiple conditions or, in other words, when multiple lines of code are used, the program becomes quite complex and very difficult to understand. Conditional operators are also known as Ternary operators in Java.
Syntax of Conditional Operator
There is a specific syntax for working with ternary or conditional operators in Java. The syntax for using ternary operators is given below. Generally, a conditional operator is used within a main(), or a specific function that is used to return certain values after the condition is executed. Once the condition is executed, then the expression which satisfies the condition is executed and returned or printed as per the user’s commands.
Variable= Expression1 ? Expression2 : Expression3
The above conditional operator works the same as an If-Else statement. The corresponding if-else statement is given below:
Syntax:
if(Expression 1)
{
Variable= Expression 2;
}
else
{
Variable= Expression 3;
}
Flowchart:
Examples of Conditional Operator in Java
Following are the examples of the conditional operator is given below:
Example #1
In example 1, we are going to see the larger between two numbers using ternary operators. We will see the input of two variables which are numbers, and then check which number is larger in that context. We can see the example below, which has been written below.
Code:
// Java program to find largest among two numbers using ternary operator
import java.io.*;
class Ternary
{
public static void main(String[] args)throws IOException
{
// Taking user input of the variables
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter first number " );
int n1= Integer.parseInt(br.readLine());
System.out.println("Enter second number " );
int n2= Integer.parseInt(br.readLine());
int max;//Variable to store the larger of the two values
// Larger among n1 and n2
max = (n1 > n2) ? n1 : n2; //Using ternary operator
// Print the largest number
System.out.println("Larger of the two numbers is " + max);
}
}
In the above code, we see how two numbers are taken from a user, and then the operation is performed to calculate the larger among the two numbers. We will see two outputs produced by the operation. First, we will enter the numbers as (100,200), and then we will enter the numbers as (500, 200). We will see the output accordingly.
Output:
Example #2
In example 2, we will see an operation that is done on two variables, both of which are numbers. If the first number is greater than the second number, then both the numbers are added and printed as given by the user. However, if the first number is less than the second number, then the second number is subtracted from the first number, and the result is printed.
Code:
// Java code to illustrate ternary operator
import java.io.*;
class Ternary2
{
public static void main(String[] args)throws IOException
{
// variable declaration
int res;
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println(" Enter First number: ");
int n1= Integer.parseInt(br.readLine());
System.out.println(" Enter Second number: ");
int n2= Integer.parseInt(br.readLine());
// Performing ternary operation
res = (n1 > n2) ? (n1 + n2) : (n1 - n2);//Calculating the sum
// Print the result
System.out.println("Result = " + res);
}
}
We are going to see a single output in this case. We are going to enter two numbers as (100,50). As the first number is greater than the second number, the program should print the summation of the two variables, which is the output. We see the sample output, which is shown below.
Output:
Example #3
In the third example, we will see the input of three numbers from the user, and we are going to check the greatest of the three numbers. Similarly, we can find the lowest of the three numbers using similar logic. The advantage of using a ternary operator is that it saves a lot amount of code within the program, and it makes the code run very fast and smoothly.
Code:
//Program to Find greatest of three numbers using Conditional Operator
import java.io.*;
public class ConditionalOperator
{
public static void main(String[] args)throws IOException
{
int a,b,c,result;
//Taking input from the user
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the Three Number : "); //prompt for input
a=Integer.parseInt(br.readLine()); //Read First number
b=Integer.parseInt(br.readLine()); //Read Second number
c=Integer.parseInt(br.readLine()); //Read third number
//Calculate the result based on conditional operator
result = (a>b)? ((a>c)?a:c) : ((b>c)?b:c);
System.out.println( result + " is Greatest");//Printing the greatest number
}
}
Now, we will input three numbers (100,200,300), and we will see the largest of the three numbers, which the program will print. According to the program, the largest of the three numbers is printed, and 300 is the largest, which is printed smoothly. Thus the program runs perfectly.
Output:
Example #4
In example 4, we are going to check the smallest of three numbers that the user has entered. The sample code is given below.
Code:
//Program to Find greatest of three numbers using Conditional Operator
import java.io.*;
public class ConditionalOperator
{
public static void main(String[] args)throws IOException
{
int a,b,c,result;
//Taking input from the user
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the Three Number : "); //prompt for input
a=Integer.parseInt(br.readLine()); //Read First number
b=Integer.parseInt(br.readLine()); //Read Second number
c=Integer.parseInt(br.readLine()); //Read third number
//Calculate the result based on conditional operator
result = (a<b)? ((a<c)?a:c) : ((b<c)?b:c);
System.out.println( result + " is Lowest");//Printing the greatest number
}
}
We are going to enter three numbers and check the lowest of them. The three numbers are (25,50,75), and the lowest number should be 25, as shown in the below output.
Output:
Conclusion
In this article, we come across different programs highlighting different aspects of Conditional Operators, and we see the different functionalities along with the different advantages of using Conditional Operators. Conditional Operators are unique when it comes to programming in the Java programming language. They are a replacement of the If-Else condition and perform smooth execution of statements based on the program’s conditions.
Recommended Articles
This is a guide to Conditional Operator in Java. Here we discuss the Introduction and syntax along with different examples and its code implementation. You may also have a look at the following articles to learn more –