The else-if ladder
20 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)An else-if ladder checks several conditions in order, from the top. Java runs the block of the first condition that is true and skips everything below it.
The else-if ladder
A grade card turns marks into A+, A, B, C or F, but one if-else gives only two answers. An else-if ladder asks several questions from the top and runs the block of the first one that is true.
- Line 1: no condition was true
Example 1 · Marks to grade
import java.util.Scanner;
public class GradeCard { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter marks: "); int marks = sc.nextInt(); if (marks >= 90) { System.out.println("Grade: A+"); } else if (marks >= 75) { System.out.println("Grade: A"); } else if (marks >= 60) { System.out.println("Grade: B"); } else if (marks >= 35) { System.out.println("Grade: C"); } else { System.out.println("Grade: F"); } sc.close(); }}Enter marks: 78 Grade: A
What this code does
78 >= 90isfalse, so Java moves down.78 >= 75istrue:Grade: Aprints and the ladder ends.- The A check needs no
&& marks < 90: Java only reaches it whenmarks >= 90was alreadyfalse.
After that block, Java jumps past the whole ladder. The final else runs only when every check above it was false.
Order matters
A teacher writes the pass check first, if (marks >= 35), and the A+ check further down. A student with 95 marks gets a C. Why?
Java stops at the first true check, and 95 is also >= 35. The checks below never get a turn. For ranges like grades, put the strictest check first (the highest marks) and work down. Slide the marks below and compare the two orders.
Experiment
Right order vs wrong order
Slide the marks. The code shown is the wrong order: it never prints A+, A or B.
- Wrong order (35 first)
- C
- Right order (90 first)
- A+
Every path gives a value
A bus ticket costs Rs 50 under 12, Rs 60 at 60 and above, and Rs 100 for everyone else. Declare price before the ladder, give it a value in every path, then print it once.
Example 2 · Ticket price stored in a variable
import java.util.Scanner;
public class BusTicket { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter age: "); int age = sc.nextInt(); int price; if (age < 12) { price = 50; } else if (age >= 60) { price = 60; } else { price = 100; } System.out.println("Ticket: Rs " + price); sc.close(); }}Enter age: 65 Ticket: Rs 60
What this code does
- Every path gives
pricea value, so Java allows the print after the ladder. - The final
elsecovers every age from 12 to 59, the ages the two checks do not catch.
The final else matters here: without it, some ages leave price empty, and Java refuses to print it with variable price might not have been initialized.
A ladder or separate ifs?
A ladder and a row of separate if statements look alike, but they behave differently. In a ladder only the first true block runs; separate if statements are each checked on their own.
Example 3 · Same checks, different behaviour
public class LadderOrIfs { public static void main(String[] args) { int marks = 95; if (marks >= 90) { System.out.println("Ladder: A+"); } else if (marks >= 75) { System.out.println("Ladder: A"); } if (marks >= 90) { System.out.println("Separate: A+"); } if (marks >= 75) { System.out.println("Separate: A"); } }}Ladder: A+ Separate: A+ Separate: A
What this code does
- The ladder stops after its first
truecheck, so it prints one grade. - The two separate
ifstatements are bothtruefor 95, so both lines print: a student would get two grades.
Use a ladder when the answers rule each other out, like one grade per student. Use separate if statements when each check is its own decision, like the Fizz and Buzz checks in The if statement (opens in a new tab).
Next step
Check your understanding
9 quick questions