Skip to content

If, Else If, and Else – Making Decisions in Java

Seanti edited this page Mar 24, 2025 · 1 revision

Just like in real life, Java needs a way to make decisions. If-else statements allow your program to take different actions based on conditions.

Imagine school grade levels:

  • If your grade is 13 or higher, you're in college.
  • Else if your grade is 11 or 12, you're in senior high school.
  • Else if your grade is 7 to 10, you're in junior high school.
  • Else if your grade is 1 to 6, you're in elementary.
  • Else, you’re not in school yet!

In Java, we use if, else if, and else to create this decision-making logic.


1. Basic If-Else Example

Let’s check what school level a student is in based on their grade:

int grade = 10; // The grade level of the student

if (grade >= 13) {
    System.out.println("You are in college.");
} else if (grade >= 11) {
    System.out.println("You are in senior high school.");
} else if (grade >= 7) {
    System.out.println("You are in junior high school.");
} else if (grade >= 1) {
    System.out.println("You are in elementary.");
} else {
    System.out.println("You are not in school yet.");
}

How It Works:

  • If grade is 13 or more, we print "You are in college." and skip the rest.
  • Else if grade is 11 or 12, we print "You are in senior high school.".
  • Else if grade is 7 to 10, we print "You are in junior high school.".
  • Else if grade is 1 to 6, we print "You are in elementary.".
  • Else, we print "You are not in school yet.".

Example Outputs:

Input (grade) Output
14 "You are in college."
11 "You are in senior high school."
8 "You are in junior high school."
5 "You are in elementary."
0 "You are not in school yet."

2. Why Not Use Multiple if Statements?

Some beginners write multiple if statements like this:

int grade = 10;

if (grade >= 13) {
    System.out.println("You are in college.");
}
if (grade >= 11) {
    System.out.println("You are in senior high school.");
}
if (grade >= 7) {
    System.out.println("You are in junior high school.");
}
if (grade >= 1) {
    System.out.println("You are in elementary.");
}

What’s Wrong Here?

Even if grade = 14, Java checks all four if-statements, even though the first one already tells us they are in college. This is inefficient!


✅ Correct Way: Using if-else if-else (One Flow)

int grade = 10;

if (grade >= 13) {
    System.out.println("You are in college.");
} else if (grade >= 11) {
    System.out.println("You are in senior high school.");
} else if (grade >= 7) {
    System.out.println("You are in junior high school.");
} else if (grade >= 1) {
    System.out.println("You are in elementary.");
} else {
    System.out.println("You are not in school yet.");
}

Why is This Better?

  • If the student is in college, we print only "You are in college." and skip the rest.
  • If the student is in senior high, we print "You are in senior high school." and skip checking for junior high.
  • If the student is in junior high, we print "You are in junior high school." and skip everything else.

Each student only gets checked once!


3. When Should We Use Multiple if Statements?

There are times when using separate if statements is correct.

Example: When Conditions Are Independent

boolean isRaining = true;
boolean isCold = true;

if (isRaining) {
    System.out.println("Bring an umbrella.");
}
if (isCold) {
    System.out.println("Wear a jacket.");
}

Why is This Correct?

  • Whether it’s raining does not affect whether it’s cold.
  • Both messages should be printed independently if both are true.

Example Output:

Bring an umbrella.  
Wear a jacket.  

Both if statements run because they are unrelated!


4. Quick Recap – When to Use if-else vs if if

Approach When to Use It Example
if-else if-else When conditions are connected (one decision path) Checking grade level
if if if When conditions are independent (not part of the same decision flow) Checking if it's raining and if it's cold

Clone this wiki locally