0Pricing
Java Academy · Lesson

Nesting Ternaries

Chain decisions carefully.

Nesting Ternaries is a free Java Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is a Nested Ternary?

A nested ternary places another ternary inside one of its branches.

This lets you choose between three or more outcomes instead of just two.

The result is a chain of conditions, evaluated from left to right.

int score = 75;
String grade = score >= 90 ? "A" : score >= 80 ? "B" : "C";

How It Groups

The ternary is right-associative. That means the false branch swallows the rest of the chain.

So a ? x : b ? y : z reads as a ? x : (b ? y : z).

Each : hands off to the next condition.

// reads as: a ? x : (b ? y : z)
String r = false ? "x" : true ? "y" : "z"; // r = "y"

A Three-Way Choice

Here a number is classified as negative, zero, or positive.

The first condition checks negative; its false branch checks for zero versus positive.

public class Main {
    public static void main(String[] args) {
        int n = 0;
        String sign = n < 0 ? "negative" : n == 0 ? "zero" : "positive";
        System.out.println(sign);
    }
}

Grading Example

A classic use is mapping a score to a letter grade.

Each condition is checked in order until one is true, and the matching letter is returned.

public class Main {
    public static void main(String[] args) {
        int score = 83;
        String grade = score >= 90 ? "A"
            : score >= 80 ? "B"
            : score >= 70 ? "C" : "F";
        System.out.println(grade);
    }
}

Order Matters

Conditions are tested top to bottom, so put the most specific or highest range first.

If you checked score >= 70 before score >= 90, a score of 95 would wrongly stop at the first match.

// wrong order: 95 would match >= 70 first
int score = 95;
String g = score >= 70 ? "C" : score >= 90 ? "A" : "F"; // g = "C"!

Adding Parentheses for Clarity

Java does not require parentheses, but they make the nesting obvious to readers.

Wrapping each inner ternary shows exactly how the branches pair up.

int n = 5;
String s = n < 0 ? "neg" : (n == 0 ? "zero" : "pos");

Formatting Across Lines

Long chains read better when each condition sits on its own line.

Line up the ? and : so the structure looks like a ladder.

public class Main {
    public static void main(String[] args) {
        int temp = 22;
        String label = temp >= 30 ? "hot"
            : temp >= 20 ? "warm"
            : temp >= 10 ? "cool"
            : "cold";
        System.out.println(label);
    }
}

Nesting in the True Branch

You can also nest inside the true branch, though it is less common and harder to read.

When you do, parentheses are strongly recommended to avoid confusion.

boolean a = true, b = false;
String r = a ? (b ? "both" : "only a") : "neither";

Returning a Nested Ternary

A method can return a multi-way result with a single nested ternary.

This stays readable for three or four cases but grows messy beyond that.

public class Main {
    static String size(int len) {
        return len < 5 ? "short" : len < 10 ? "medium" : "long";
    }
    public static void main(String[] args) {
        System.out.println(size(7));
    }
}

Know When to Stop

Two levels of nesting are usually fine. Three or more often becomes hard to follow.

When a chain gets long, an if/else if ladder or a switch is clearer and easier to debug.

Tracing a Chain

Walk through this chain by hand: with day = 6, the first condition is false, the second is true.

So the result is "Saturday" without checking the rest.

public class Main {
    public static void main(String[] args) {
        int day = 6;
        String name = day == 7 ? "Sunday"
            : day == 6 ? "Saturday" : "Weekday";
        System.out.println(name);
    }
}

Quick Check

Test how nested ternaries group.

Recap

Nested ternaries chain conditions to choose among three or more values.

They are right-associative, so the false branch absorbs the rest; order your conditions carefully.

Add parentheses and line breaks for clarity, and switch to if/else when chains grow long.

Frequently asked questions

Is the “Nesting Ternaries” lesson free?

Yes — the full text of “Nesting Ternaries” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Nesting Ternaries”?

Chain decisions carefully. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Nesting Ternaries” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. The Ternary Operator
  2. Nesting Ternaries
  3. Ternary vs if/else
  4. Readability Pitfalls
← Back to Java Academy