🔀 Control Flow · Beginner

if / else if / else in Java

Branching on boolean conditions.

🧩 The mysteryYour grading app gives a 95 a plain "Pass" instead of "Excellent". No typos, no crash, the code compiles. So who stole the Excellent?

A fork in the road

An if runs its block only when its condition is true. The condition must be a real boolean (true or false). Unlike C, Java never treats 1 as true or 0 as false: if (1) is a compile error.

int temp = 30;
if (temp > 25) {
    System.out.println("Hot!");
}

Chains: the first true wins

Add else if to test more conditions, top to bottom. The first true branch runs and the rest of the chain is skipped, even if later conditions are also true. A final else catches whatever matched nothing. else is optional, and at most one branch of a chain ever runs.

int score = 72;
if (score >= 90) {
    System.out.println("A");
} else if (score >= 70) {
    System.out.println("B");
} else {
    System.out.println("C");
}
🔮 Predict it

Your turn

15 is greater than 10 AND greater than 0. What prints?

int t = 15;
if (t > 30) {
    System.out.println("hot");
} else if (t > 10) {
    System.out.println("warm");
} else if (t > 0) {
    System.out.println("cool");
}
  1. warm
  2. warm cool
  3. cool
  4. hot
Show the answer

Just warm. 15 > 30 is false, 15 > 10 is true, so that branch runs and the chain stops. The t > 0 test is never even checked.

Order matters: specific first

✗ Broad test first
if (score >= 50) {
    grade = "Pass";
} else if (score >= 90) {
    grade = "Excellent";
}

95 >= 50 is true, so Pass wins. The Excellent branch can never run for high scores.

✓ Specific test first
if (score >= 90) {
    grade = "Excellent";
} else if (score >= 50) {
    grade = "Pass";
}

Put the narrowest condition first, so it gets a chance before the broad one swallows everything.

⚠️ The trap

The = vs == typo

= assigns, == compares. if (x = 5) doesn't compile: the assignment's value is the int 5, not a boolean. But with a boolean variable the typo compiles: done = true assigns true, and the whole expression's value is true, so the branch always runs.

int x = 3;
if (x = 5) { }       // compile error
boolean done = false;
if (done = true) { } // compiles! always runs
🔮 Predict it

Spot the sneaky typo

ready starts as false. What prints?

boolean ready = false;
if (ready = true) {
    System.out.println("go");
}
System.out.println(ready);
  1. true
  2. go true
  3. Nothing is printed
  4. go false
Show the answer

ready = true assigns true, and that value (true) is the condition, so go prints. Worse, ready is now permanently true. Always compare booleans with if (ready), never == true.

💼 In the real world

In real projects

Pricing rules, discount tiers, access checks: business logic is full of if/else if chains. A classic production bug is a general rule placed above a specific one, so every customer silently gets the smallest discount. Reviewers read chains top-down asking: can every branch actually be reached?

Key takeaways

  1. The condition must be a boolean; Java never treats 0 or 1 as false/true
  2. Conditions are checked top to bottom; only the first true branch runs
  3. else is optional, and at most one branch of a chain executes

💡 Like a bouncer checking rules top to bottom: the first rule that matches decides, and nobody reads the rest.

🤯 Did you know?

C programmers invented "Yoda conditions" like if (5 == x) so that typing = by mistake fails to compile. Java mostly doesn't need the trick: if (x = 5) is already an error because an int isn't a boolean.

Practice questions

What does this print?

int score = 85;
if (score >= 90) {
    System.out.println("A");
} else if (score >= 80) {
    System.out.println("B");
} else if (score >= 70) {
    System.out.println("C");
}
  1. A
  2. B
  3. B C
  4. C
Check your answer

B. 85 >= 90 is false, 85 >= 80 is true, so B prints. The chain stops there, even though 85 >= 70 is also true.

What does this print?

int score = 95;
if (score >= 50) {
    System.out.println("Pass");
} else if (score >= 90) {
    System.out.println("Excellent");
}
  1. Excellent
  2. Pass Excellent
  3. Pass
  4. Nothing is printed
Check your answer

Pass. The first true condition wins. score >= 50 is checked first and is true, so the Excellent branch can never be reached for high scores. Put the most specific test first.

Next: a variable you declared two lines ago suddenly "doesn't exist". Where do variables go when a block ends?