🌱BeginnerControl Flow
if / else Statements
Make decisions in your Java programs using if and else - the most fundamental control flow structure.
Overview
Programs need to make decisions based on conditions. Java's if statement executes a block of code only when a boolean condition is true. The optional else block runs when the condition is false. Together they let your program take different paths based on data.
The condition inside if must evaluate to a boolean (true or false). You can use any comparison or logical expression, or a boolean variable directly. The code block between { } executes only if the condition is met.
Key Points
- •if (condition) { ... } - executes the block when condition is true
- •else { ... } - optional; executes when the if condition is false
- •The condition must be a boolean expression - anything that produces true or false
- •Curly braces {} are technically optional for single-statement if/else, but always use them to avoid bugs
- •if/else is always mutual - exactly one branch executes, never both
- •Nested if: an if statement inside another if's block (use sparingly to avoid deep nesting)
Examples
Basic if/else
public class Main {
public static void main(String[] args) {
int temperature = 22;
if (temperature > 30) {
System.out.println("It's hot outside!");
} else {
System.out.println("It's not too hot.");
}
// Single-value boolean
boolean isRaining = false;
if (isRaining) {
System.out.println("Take an umbrella");
} else {
System.out.println("No umbrella needed");
}
}
}The else block runs only if the if condition is false - never both blocks run.
Nested if statements
public class Main {
public static void main(String[] args) {
int age = 20;
boolean hasTicket = true;
if (age >= 18) {
if (hasTicket) {
System.out.println("Welcome! Enjoy the show.");
} else {
System.out.println("You need a ticket to enter.");
}
} else {
System.out.println("You must be 18 or older.");
}
}
}Nested ifs can often be rewritten with && to reduce nesting depth.
The "dangling else" pitfall - always use braces
// DANGEROUS: which if does the else belong to?
if (x > 0)
if (x > 10)
System.out.println("Greater than 10");
else // ← this belongs to the INNER if, not outer!
System.out.println("This is confusing");
// SAFE: always use braces
if (x > 0) {
if (x > 10) {
System.out.println("Greater than 10");
}
} else {
System.out.println("x is 0 or negative");
}Java's "dangling else" rule: an else always pairs with the nearest preceding if. Always use braces to make intent explicit.
Common Mistakes
- Using = instead of == in the condition: `if (x = 5)` is an assignment, not a comparison - and it causes a compile error since int is not boolean
- Omitting curly braces and accidentally including too many or too few statements in the if block
- Putting a semicolon after the if condition: `if (x > 0);` - the semicolon creates an empty if body, so the block always runs
Note
Prefer positive conditions: `if (isValid) { ... }` is more readable than `if (!isInvalid) { ... }`. Positive logic is easier for readers to follow.
Exercises
Write Java code and click Run to execute it instantly. When your output looks correct, mark as done.
Loading editor…
Completed: 0 / 4 exercises
Knowledge Check
Question 1 of 4