Skip to lesson
MentorNxt home

Java Programming

Conditional Statements

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.

Syntax
if (condition1) { statements }
else if (condition2) { statements }
else if (condition3) { statements }
else { statements }
  1. Line 1: no condition was true
marks is 78. Java checks from the top: 78 >= 90 is false, 78 >= 75 is true, so grade A is chosen. The checks below it and the final else are skipped.

Example 1 · Marks to grade

GradeCard.javaJava
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();    }}
Sample output
Enter marks: 78
Grade: A

What this code does

  • 78 >= 90 is false, so Java moves down. 78 >= 75 is true: Grade: A prints and the ladder ends.
  • The A check needs no && marks < 90: Java only reaches it when marks >= 90 was already false.

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.

int marks = 95;
if (marks >= 35) {
System.out.println("C");
} else if (marks >= 60) {
System.out.println("B");
} else if (marks >= 75) {
System.out.println("A");
} else if (marks >= 90) {
System.out.println("A+");
} else {
System.out.println("F");
}
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

BusTicket.javaJava
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();    }}
Sample output
Enter age: 65
Ticket: Rs 60

What this code does

  • Every path gives price a value, so Java allows the print after the ladder.
  • The final else covers 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

LadderOrIfs.javaJava
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");        }    }}
Output
Ladder: A+
Separate: A+
Separate: A

What this code does

  • The ladder stops after its first true check, so it prints one grade.
  • The two separate if statements are both true for 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