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.
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.
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.
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.
How if/else Flows
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.
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.
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.
How to Write a Conditional
Example 1: Even or Odd
Determine whether an integer is even or odd using the modulo operator %.
Example 2: Grade Calculator
Assign a letter grade based on a numeric score. Notice conditions go from highest to lowest.
Example 3: Ticket Pricing
A theme park charges different prices by age group. This example combines an else-if chain with compound conditions using &&.
Boolean Operators in Conditions
Combine simple conditions into more powerful expressions using logical operators.
age >= 13 && hasTicket
day.equals("Sat") || day.equals("Sun")
!isGameOver
Common Mistakes
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
int x = 15; if (x > 20) { System.out.print("A"); } else if (x > 10) { System.out.print("B"); } else { System.out.print("C"); }
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
int score = 95; if (score >= 70) { System.out.print("C"); } else if (score >= 80) { System.out.print("B"); } else if (score >= 90) { System.out.print("A"); }
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
int temp = 72; boolean raining = true; if (temp > 80 && !raining) { System.out.print("Beach day!"); } else if (temp > 60) { System.out.print("Nice walk"); } else { System.out.print("Stay inside"); }
What does this code print?