01 / 21
T F

If/Else & Else-If
Chains in Java

Master conditional logic: learn how to make your programs choose different paths based on boolean expressions using if, else, and else-if chains.

Java Control Flow

What Is Conditional Logic?

Programs often need to make decisions. Should the player gain a life or lose one? Should the app show a discount or the full price? Conditional logic lets your code branch: it evaluates a boolean expression and runs different blocks depending on whether the result is true or false.

Boolean Expression
Any expression that evaluates to true or false, such as score >= 90.
Branch
A separate code path the program may follow depending on the condition's result.

The if Statement

The simplest conditional: execute a block of code only when a condition is true. If the condition is false, the block is skipped entirely.

if (temperature > 100) { System.out.println("Water is boiling!"); }

Adding the else Branch

An else block provides a fallback: code that runs when the condition is false. Together, if/else guarantees exactly one of the two blocks will execute.

if (age >= 18) { System.out.println("You can vote."); } else { System.out.println("Too young to vote."); }

How if/else Flows

Start condition? (boolean) true if block executes false else block executes Continue

The else-if Chain

When there are more than two possibilities, chain else if clauses. Java tests each condition top to bottom and executes the first block whose condition is true. If none match, the optional else at the end runs.

if (score >= 90) { grade = "A"; } else if (score >= 80) { grade = "B"; } else if (score >= 70) { grade = "C"; } else { grade = "F"; }

First Match Wins

In an else-if chain, only the first true condition's block executes. All subsequent conditions are skipped, even if they are also true. This is why the order of conditions matters.

F F F else T T T Remaining conditions are never checked.
\(\text{score} \ge 90\text{?}\)
\(\text{score} \ge 80\text{?}\)
\(\text{score} \ge 70\text{?}\)
\(\text{grade} = \text{"A"}\)
\(\text{grade} = \text{"B"}\)
\(\text{grade} = \text{"C"}\)
\(\text{grade} = \text{"F"}\)
Example: \(\text{score} = 85\)
\(85 \ge 90\text{?}\) false \(\rightarrow\) \(85 \ge 80\text{?}\) true \(\rightarrow\) \(\text{grade} = \text{"B"}\) (stops here)

Separate if vs. else-if

Using multiple separate if statements is not the same as an else-if chain. Separate ifs are independent: every condition is checked. In an else-if chain, checking stops at the first match.

Separate ifs else-if Chain
Conditions tested All of them, always Only until first true
Blocks that can run Multiple (0 to N) Exactly one (or zero)
Use when Conditions are independent Conditions are mutually exclusive
Performance More comparisons Fewer comparisons (short-circuits)

Nested if Statements

You can place an if inside another if. The inner condition is only evaluated when the outer condition is true. Use nesting when a second decision depends on the first.

if (hasTicket) { if (age >= 13) { System.out.println("Enjoy the movie!"); } else { System.out.println("Too young for this film."); } } else { System.out.println("Please buy a ticket first."); }

How to Write a Conditional

1
Identify the decision. What question must the program answer?
2
Write the boolean expression. Use relational operators: ==, !=, <, >, <=, >=.
3
Determine how many branches. Two outcomes: if/else. Three or more: else-if chain.
4
Order conditions from most specific to least. Place stricter checks first so the first-match rule gives the correct result.
5
Add a final else (safety net). Handle unexpected or default cases to avoid silent failures.

Example 1: Even or Odd

Determine whether an integer is even or odd using the modulo operator %.

int num = 7; if (num % 2 == 0) { System.out.println(num + " is even"); } else { System.out.println(num + " is odd"); } // Output: 7 is odd

Example 2: Grade Calculator

Assign a letter grade based on a numeric score. Notice conditions go from highest to lowest.

int score = 76; String grade; if (score >= 90) { grade = "A"; } else if (score >= 80) { grade = "B"; } else if (score >= 70) { grade = "C"; } else if (score >= 60) { grade = "D"; } else { grade = "F"; } // grade = "C" (76 >= 70 is the first true match)

Example 3: Ticket Pricing

A theme park charges different prices by age group. This example combines an else-if chain with compound conditions using &&.

int age = 25; double price; if (age < 3) { price = 0.0; // Free } else if (age <= 12) { price = 25.0; // Child } else if (age <= 17) { price = 35.0; // Teen } else if (age >= 65) { price = 30.0; // Senior } else { price = 50.0; // Adult } // price = 50.0 (age 25 is an adult)

Boolean Operators in Conditions

Combine simple conditions into more powerful expressions using logical operators.

&& AND
Both sides must be true.
age >= 13 && hasTicket
|| OR
At least one side must be true.
day.equals("Sat") || day.equals("Sun")
! NOT
Flips true to false and vice versa.
!isGameOver

Common Mistakes

✕ Using = instead of ==
if (x = 5) is assignment, not comparison. Use == for equality.
✕ Wrong condition order
Checking score >= 70 before score >= 90 means scores of 95 get "C" instead of "A".
✕ Missing curly braces
Without {}, only the first statement after the if is conditional. The rest always runs.
✕ Comparing Strings with ==
== checks reference, not value. Use .equals() for String comparison.

Summary Cheat Sheet

Pattern Syntax When to Use
Simple if if (cond) { } One possible action
if/else if (cond) { } else { } Two mutually exclusive outcomes
else-if chain if ... else if ... else Three or more exclusive outcomes
Nested if if (a) { if (b) { } } Decision depends on a prior decision
Compound condition if (a && b) Multiple criteria must be met at once

Question 1 of 5

What does this code print?

Question 2 of 5

Which operator should you use to compare two String values for equality in Java?

Question 3 of 5

What does this code print when score = 95?

Question 4 of 5

What is the key difference between using separate if statements and an else-if chain?

Question 5 of 5

What does this code print?