Ternary Operator

The conditional operator chooses one of two values: condition ? valueIfTrue : valueIfFalse.

It works best when both alternatives are short and the result is assigned or returned. A full if statement is clearer when either branch needs several actions.

Choose one value

public class Main {
    public static void main(String[] args) {
        int score = 85;
        String result = score >= 70 ? "PASS" : "RETRY";
        System.out.println("result: " + result);
    }
}

Output:

result: PASS

The condition checks the passing threshold. Since 85 is at least 70, the selected string becomes the value of result.

Nested ternary expressions become difficult to scan. Prefer an if chain or a switch expression for more than two meaningful outcomes.

Use the ternary operator to choose Even or Odd.