Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java ReferenceJava Reference1

Java if-else-if Statement
Syntax and Examples



In Java, if-else-if ladder is used to evaluate multiple conditions in a structured manner. If you’ve already understood simple if and if-else blocks, this is the next natural step.

The if-else-if ladder allows you to test multiple conditions, one after another. As soon as one condition evaluates to true, the corresponding block is executed, and the rest are skipped.

Syntax of if else if in Java

if (condition1) {
    // code block 1
} else if (condition2) {
    // code block 2
} else if (condition3) {
    // code block 3
} else {
    // default code block
}

Example 1: Grade Classification

public class GradeExample {
    public static void main(String[] args) {
        int score = 85;

        if (score >= 90) {
            System.out.println("Grade: A");
        } else if (score >= 80) {
            System.out.println("Grade: B");
        } else if (score >= 70) {
            System.out.println("Grade: C");
        } else {
            System.out.println("Grade: D or lower");
        }
    }
}
Grade: B

Explanation:

Here, score is 85. It first checks if it's greater than or equal to 90 (false), then checks if it's greater than or equal to 80 (true). So, it prints "Grade: B" and exits the ladder.

Example 2: Weather Message

public class WeatherExample {
    public static void main(String[] args) {
        int temperature = 5;

        if (temperature > 30) {
            System.out.println("It's a hot day!");
        } else if (temperature > 20) {
            System.out.println("It's a warm day.");
        } else if (temperature > 10) {
            System.out.println("It's a bit chilly.");
        } else {
            System.out.println("It's cold outside.");
        }
    }
}
It's cold outside.

Explanation:

In this program, we're using a series of if-else if-else conditions to print a message based on the current temperature.

Therefore, it prints: It's cold outside.

This type of conditional logic is useful for decision-making based on numeric ranges or categories.

Example 3: Nested Conditions

public class NestedIfElseIf {
    public static void main(String[] args) {
        int marks = 78;
        char section = 'A';

        if (marks >= 75) {
            if (section == 'A') {
                System.out.println("Eligible for Scholarship in Section A");
            } else if (section == 'B') {
                System.out.println("Eligible for Scholarship in Section B");
            } else {
                System.out.println("Eligible but section not identified");
            }
        } else {
            System.out.println("Not eligible for scholarship");
        }
    }
}
Eligible for Scholarship in Section A

Explanation:

This program demonstrates how nested if-else conditions work in Java. The outer if checks whether the student has scored 75 or more marks. If this condition is true, the program then checks the section to determine the specific eligibility message:

In this specific case, since marks = 78 and section = 'A', the output is:

Eligible for Scholarship in Section A

Common Mistakes to Avoid When Using if else if

When to Use if-else-if Ladder

Use if-else-if when:

Summary

The if-else-if structure in Java is all about making your program responsive to different inputs and situations. By practicing with everyday logic like grading systems, weather classifications, or eligibility checks, you build the instinct to apply this structure naturally in your code.

Try It Yourself!

Change the values in the above examples and see how the outputs differ. Experiment with adding new conditions and see where they fall in the ladder. This is the best way to gain confidence with flow control.

QUIZ

Question 1:How does Java’s if-else-if ladder behave when multiple conditions evaluate to true?

Question 2:In an if-else-if structure, the else block is mandatory and must always be included.

Question 3:Which of the following are true about the Java if-else-if ladder?

Question 4:What will be the output of the following code?

int score = 85;

if (score >= 90) {
    System.out.println("Grade: A");
} else if (score >= 80) {
    System.out.println("Grade: B");
} else if (score >= 70) {
    System.out.println("Grade: C");
} else {
    System.out.println("Grade: D or lower");
}

Question 5:The Java if-else-if ladder can be replaced with a switch statement in all cases.

Question 6:Analyze the output of this program:

int temperature = 5;

if (temperature > 30) {
    System.out.println("It's a hot day!");
} else if (temperature > 20) {
    System.out.println("It's a warm day.");
} else if (temperature > 10) {
    System.out.println("It's a bit chilly.");
} else {
    System.out.println("It's cold outside.");
}



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M