Ternary Operator if-else Statement

What is Ternary Operator if-else statement in Java?

The ternary operator is a shorthand conditional expression with three arguments. The first argument is a condition that determines the comparison, the second argument provides the result when the comparison is true, and the third argument provides the result when the comparison is false. It allows for a more concise way of writing if-else statements and is helpful in simplifying code when dealing with simple conditions.

Syntax:

condition ? expression1 : expression2;

Explanation of this code:

The condition is a boolean expression that is evaluated.

If the condition is true, the value of expression1 is returned.

If the condition is false, the value of expression2 is returned.

The ternary operator can be handy in situations where the condition and expressions are straightforward and do not involve complex logic or multiple branches. However, for more complex scenarios, using if-else statements might be more readable and maintainable.

Three operands are required for the ternary operator: condition, expression1, and expression 2. Hence, the name ternary operator.

Java Ternary operator if-else Statement Example

Example 01:
package com.example.myapp; // Package name

public class TernaryOperatorExample {
    public static void main(String[] arg) {

        // here we declare a variable
        int numToCheck = 17;

        // Using the ternary operator
        String result = (numToCheck % 2 == 0) ? "even number" : "odd number"; // like single line if-else statement

        System.out.println(result);

        // ternary operator ?: if the condition is true, it will take the value before the colon
        // if the condition is false, it will take the value after the colon
    }
}

Output:

odd number

Explanation of this code:

  • The code initializes an integer variable named “numToCheck” with a value of 17. This variable represents the number that needs to be checked for evenness or oddness.
  • The code uses the ternary operator ? :, which is a shorthand way of writing a simple if-else statement.
  • The ternary operator works as follows:
  • The condition inside the parentheses (numToCheck % 2 == 0) checks whether “numToCheck” is divisible by 2 without any remainder. If the condition is true, it means “numToCheck” is even; otherwise, it’s odd.
  • If the condition is true (numToCheck is even), the value before the colon : is assigned to the variable “result,” which is “even number.”
  • If the condition is false (numToCheck is odd), the value after the colon : is assigned to the variable “result,” which is “odd number.”
  • After evaluating the ternary operator, the value of “result” will be either “even number” or “odd number” based on whether “numToCheck” is even or odd, respectively.
  • The program then prints the value of “result” to the console using the System.out.println() statement.

In summary, the code checks if the number “numToCheck” is even or odd using the ternary operator and then prints the result to the console. If “numToCheck” is even, it prints “even number,” and if it’s odd, it prints “odd number.” In this case, since “numToCheck” is 17, which is an odd number, the program will output “odd number” to the console.

Example 02:
package com.example.myapp; 

public class DuplicateTest {

    public static void main(String[] args) {
        int userAge = 25;
        String votingMessage = (userAge > 18) ? "You are eligible to vote" : "You are not eligible to vote";
        System.out.println(votingMessage);
    }
}

Output:

You are eligible to vote
Explanation of this code:
  • int userAge = 25; :- This line declares an integer variable named “userAge” and assigns it a value of 25. The variable “userAge” represents the age of a person.
  • String votingMessage = (userAge > 18) ? “You are eligible to vote” : “You are not eligible to vote”; :- This line uses the ternary operator ? : to assign a message to the string variable “votingMessage” based on the condition “userAge > 18.” If “userAge” is greater than 18, it assigns the string “You are eligible to vote” to “votingMessage.” Otherwise, it assigns the string “You are not eligible to vote.”
  • println(votingMessage); :- This line prints the value of the “votingMessage” variable to the console. The output will be either “You are eligible to vote” or “You are not eligible to vote,” depending on the value of “userAge.” In this case, since “userAge” is 25, the output will be “You are eligible to vote.”
Visited 1 times, 1 visit(s) today

Comments are closed.

Close