Ternary Operator and Nested Ternary Operators in Java
Introduction to Ternary Operators in Java
The ternary operator, also known as the conditional operator, is a shorthand for the if-else statement. It takes three operands: a condition, an expression1, and an expression2. The condition is evaluated, and if it is true, expression1 is returned; otherwise, expression2 is returned.
The syntax for the ternary operator is as follows:
(condition) ? expression1 : expression2
The following example shows how to use the ternary operator to check if a number is even or odd:
int number = 10;
String result = (number % 2 == 0) ? "Even" : "Odd";
System.out.println(result); // Prints "Even"
The ternary operator can also be used to assign values to variables. The following example shows how to use the ternary operator to assign the value "true" to the variable isAdult if the user's age is greater than or equal to 18, and the value "false" otherwise:
Java
int age = 20;
boolean isAdult = (age >= 18) ? true : false;
System.out.println(isAdult); // Prints "true"
The ternary operator can be nested to create more complex conditional statements. For example, the following example shows how to use the ternary operator to check if a number is even or odd, and then print a different message depending on the result:
int number = 10;
String message = (number % 2 == 0) ? "The number is even." : "The number is odd.";
System.out.println(message); // Prints "The number is even."
Here are some tips for using the ternary operator effectively
- Use the ternary operator for simple conditional statements.If the conditional statement is complex, it may be better to use an if-else statement.
- Use the ternary operator to assign values to variables. This can make your code more concise and readable.
- Use parentheses to make your ternary expressions easier to read and understand.
- Avoid nesting ternary operators too deeply. This can make your code difficult to understand and maintain.
Exercises
Determine the output of the following code
package practiceproject;
public class democlass {
public static void main(String[] args) {
int a=15;
int k = a>10 ? 10 : 12;
System.out.println(k);
}
}
Output of the Code
10
Get the Mark scored in a subject as an input from the user. If mark is greater than or equal to 40 then the result should be displayed as passed and for anything less than 40 the result should be displayed as failed.Use Ternary Operators to solve this problem.
package javaprogrammingdemo;
import java.util.Scanner;
public class javalabclass{
public static void main(String args[])
{
String result;
System.out.println("Enter the Mark scored");
Scanner input = new Scanner(System.in);
int mark = input.nextInt();
result = mark>=40 ? "passed" : "failed";
System.out.println(result);
}
}
Program Output
Enter the Mark scored
50
passed
Get the Mark scored in a subject as an input from the user. If mark is greater than or equal to 40 then add 10 to the Mark,if the input mark is lesser then 40 then add 20 to the mark. Display the final mark. Use Ternary Operators to solve this problem.
package javaprogrammingdemo;
import java.util.Scanner;
public class javalabclass{
public static void main(String args[])
{
int result;
System.out.println("Enter the Mark scored");
Scanner input = new Scanner(System.in);
int mark = input.nextInt();
result = mark>=40 ? mark+10 : mark+20;
System.out.println(result);
}
}
Program Output:
Enter the Mark scored
30
50
Get the Mark as input from the user and display the Grade usign the following conditions
Print A if mark is greater than or equal to 80
Print B is mark is greater than or equal to 60 and less than 80
Print C otherwise (Use Nested Ternary Operator to Complete this)
package javaprogrammingdemo;
import java.util.Scanner;
public class javalabclass{
public static void main(String args[])
{
char result;
System.out.println("Enter the Mark scored");
Scanner input = new Scanner(System.in);
int mark = input.nextInt();
result = (mark>=80) ? 'A' : (mark>=60 && mark < 80) ? 'B' : 'C';
System.out.println(result);
}
}
Program Output
Enter the Mark scored
85
A