DevAcademia
C++C#CPythonJava
  • Java Basics

  • Java Introduction
  • Java Get Started
  • Java Syntax
  • Java Output
  • Java Comments
  • Java Variables
  • Java Data Types
  • Java Type Casting
  • Java Operators
  • Java Strings
  • Java If...Else
  • Java Switch Statement
  • Java Loops
  • Java Math
  • Java Arrays
  • Java Date
  • Java OOP

  • Java Classes/Objects
  • Java Class Attributes
  • Java Class Methods
  • Java Constructors
  • Java Destructors
  • Java this Keyword
  • Java Modifiers
  • Java Non Modifiers
  • Java Encapsulation
  • Java Packages & API
  • Java Inheritance
  • Java Polymorphism
  • Java Super Keyword
  • Java Inner Classes
  • Java Exception Handling
  • Java Abstraction
  • Java Interfaces
  • Java Enums
  • Java User Input
  • Java Quiz

  • Java Fundamentals Quiz
  • Java Basics

  • Java Introduction
  • Java Get Started
  • Java Syntax
  • Java Output
  • Java Comments
  • Java Variables
  • Java Data Types
  • Java Type Casting
  • Java Operators
  • Java Strings
  • Java If...Else
  • Java Switch Statement
  • Java Loops
  • Java Math
  • Java Arrays
  • Java Date
  • Java OOP

  • Java Classes/Objects
  • Java Class Attributes
  • Java Class Methods
  • Java Constructors
  • Java Destructors
  • Java this Keyword
  • Java Modifiers
  • Java Non Modifiers
  • Java Encapsulation
  • Java Packages & API
  • Java Inheritance
  • Java Polymorphism
  • Java Super Keyword
  • Java Inner Classes
  • Java Exception Handling
  • Java Abstraction
  • Java Interfaces
  • Java Enums
  • Java User Input
  • Java Quiz

  • Java Fundamentals Quiz

Loading Java tutorial…

Loading content
Java BasicsTopic 28 of 59
←PreviousPrevNextNext→

Java else if Statement - Complete Guide

Introduction to else if Statement

The else if statement in Java lets you evaluate multiple conditions in sequence. It prevents excessive nesting and improves readability by forming a chain of conditional checks.

Technically, else if is just an else followed by an if, but used together they provide a clean way to handle several exclusive conditions.

Basic else if Syntax

The else if construct checks conditions one by one until one evaluates to true. If none are true, the optional else block runs.

Example
public class ElseIfStatement {
    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 if (score >= 60) {
            System.out.println("Grade: D");
        } else {
            System.out.println("Grade: F");
        }

        int hour = 14; // 2 PM
        if (hour < 12) {
            System.out.println("Good morning!");
        } else if (hour < 18) {
            System.out.println("Good afternoon!");
        } else if (hour < 21) {
            System.out.println("Good evening!");
        } else {
            System.out.println("Good night!");
        }

        int number = 0;
        if (number > 0) {
            System.out.println("Positive number");
        } else if (number < 0) {
            System.out.println("Negative number");
        } else {
            System.out.println("Zero");
        }
    }
}
Output
Grade: B
Good afternoon!
Zero

Comparison with Nested if-else

Using else if is more concise and readable than chaining nested if-else statements. Order of conditions is important: once a true condition is found, later conditions are skipped.

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

        // Nested if-else
        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 {
                    if (score >= 60) {
                        System.out.println("Grade: D");
                    } else {
                        System.out.println("Grade: F");
                    }
                }
            }
        }

        // Equivalent with else if
        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 if (score >= 60) {
            System.out.println("Grade: D");
        } else {
            System.out.println("Grade: F");
        }

        // Order matters
        int value = 75;

        if (value > 50) {
            System.out.println("Greater than 50");
        } else if (value > 70) {
            System.out.println("Greater than 70"); // skipped
        }

        if (value > 90) {
            System.out.println("Greater than 90");
        } else if (value > 70) {
            System.out.println("Greater than 70");
        } else if (value > 50) {
            System.out.println("Greater than 50");
        }
    }
}
Output
Grade: B
Grade: B
Greater than 50
Greater than 70

Best Practices for else if Statements

  • ✅ Use else if to handle multiple exclusive conditions
  • ✅ Place the most specific or most likely conditions first
  • ✅ Include a final else to catch unexpected values
  • ✅ Avoid overlapping conditions that might cause ambiguity
  • ✅ Consider switch for many equality checks
  • ✅ Keep chains short for readability
  • ✅ Document tricky conditions with comments
  • ✅ Test all possible execution paths
Test your knowledge: Java else if Statement - Complete Guide
Quiz Configuration
4 of 10 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
Java BasicsTopic 28 of 59
←PreviousPrevNextNext→