Control Flow & Loops

The if / else if / else Statement

15 min Lesson 2 of 14

The if / else if / else Statement

Every program eventually needs to make a decision: run this code only if something is true, or choose between several paths depending on a value. Java's if statement is the fundamental tool for that kind of branching. You already know what a boolean expression produces (from the previous lesson); now you will learn how to act on it.

The basic if block

An if statement evaluates a boolean expression. When the expression is true, the body runs; when it is false, Java skips straight past it.

int temperature = 35; if (temperature > 30) { System.out.println("It is hot outside."); } // prints: It is hot outside.

The curly braces { } define the block — the group of statements that belong to this branch. You can put as many statements inside as you need.

Adding an else branch

An else branch provides the fallback — the code that runs when the if condition is false.

int temperature = 18; if (temperature > 30) { System.out.println("It is hot outside."); } else { System.out.println("The weather is comfortable."); } // prints: The weather is comfortable.

Exactly one of the two branches will execute — never both, never neither. That is the guarantee the if / else structure gives you.

Chaining with else if

When there are more than two possible situations, you chain additional conditions with else if. Java evaluates them top to bottom and executes the first branch whose condition is true, then skips all the rest.

int score = 74; 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"); } // prints: Grade: C
Order matters. Because Java stops at the first match, you write the most restrictive condition first. If you placed score >= 60 at the top, a score of 95 would print "Grade: D" — clearly wrong. Always think about which range is narrowest and put it first.

Nested if statements

You can place an if statement inside another if block. This is called nesting and is useful when a second decision only makes sense after the first one is resolved.

boolean hasTicket = true; int age = 17; if (hasTicket) { if (age >= 18) { System.out.println("Access granted."); } else { System.out.println("You must be 18 or older."); } } else { System.out.println("Please buy a ticket first."); } // prints: You must be 18 or older.
Keep nesting shallow. More than two levels deep is a warning sign that the logic should be extracted into a helper method or rewritten using logical operators (&&, ||) to combine conditions into a single if. Deep nesting makes code hard to read and hard to test.

The dangling-else trap

Consider this ambiguous-looking code:

int x = 5; if (x > 0) if (x > 10) System.out.println("Greater than 10"); else System.out.println("Not positive"); // which if does this belong to?

The indentation looks like the else pairs with the outer if (x > 0), but Java's rule is: an else always belongs to the nearest preceding if that does not yet have an else. So the else here pairs with if (x > 10). For x = 5, the outer condition is true but the inner one is false, so the else runs and prints "Not positive" — even though 5 is positive. This is the classic dangling-else bug.

Always use braces, even for single-line bodies. The safe fix is trivial: wrap every body in { }. Then your indentation and the compiler's interpretation can never disagree.
if (x > 0) { if (x > 10) { System.out.println("Greater than 10"); } } else { System.out.println("Not positive"); // now unambiguously belongs to the outer if }

The missing-braces trap

A related pitfall: omitting braces when you later add a second statement to a body.

boolean isAdmin = false; // original code — works fine if (isAdmin) System.out.println("Welcome, admin."); // someone adds a second line, forgetting braces if (isAdmin) System.out.println("Welcome, admin."); System.out.println("Loading admin panel..."); // ALWAYS runs — not inside the if!

The second println is outside the if entirely. Because there are no braces, only the first statement after if is conditional. "Loading admin panel..." will print for every user. This is a subtle, real-world security-style bug. The fix is the same: always use braces.

Putting it all together

Here is a small, self-contained example that uses branching to describe a traffic light:

String light = "yellow"; if (light.equals("green")) { System.out.println("Go."); } else if (light.equals("yellow")) { System.out.println("Slow down."); } else if (light.equals("red")) { System.out.println("Stop."); } else { System.out.println("Unknown signal."); } // prints: Slow down.
Use .equals() for String comparison, not ==. The == operator checks whether two variables point to the same object in memory, not whether their content is equal. For string content comparison always call .equals() or .equalsIgnoreCase().

Summary

  • if runs a block only when its condition is true.
  • else provides the fallback path when no prior condition was true.
  • else if chains additional conditions; Java picks the first match and ignores the rest.
  • Conditions are evaluated top to bottom — put the most specific check first.
  • Nesting is valid but should be kept shallow; prefer logical operators to combine conditions when possible.
  • Always wrap if, else if, and else bodies in braces to avoid the dangling-else and missing-braces traps.