Java if, else and switch: Making Decisions in Your Code
Learn how Java makes decisions with if, else if, and switch statements — clear beginner examples, when to use each, and the classic missing-break bug explained.
Up to now our programs have run straight from top to bottom, doing the same thing every time. But real programs need to make decisions: "if the user is over 18, allow access," "if the password is correct, log in." In this lesson you'll learn how Java makes choices using if, else, and switch. This is where your code starts to think.
The if statement
An if runs a block of code only when a condition is true. The condition goes in parentheses and must be a boolean (true/false) — exactly what the comparison operators from Lesson 5 give us.
int age = 20; if (age >= 18) { System.out.println("You are an adult."); }
If age >= 18 is true, the line inside the braces runs. If it's false, Java simply skips the whole block and moves on.
if...else
Add an else to say "otherwise, do this instead." Exactly one of the two blocks will always run:
int age = 15; if (age >= 18) { System.out.println("You can vote."); } else { System.out.println("Too young to vote."); }
if...else if...else (multiple choices)
When you have more than two possibilities, chain them with else if. Java checks each condition in order and runs the first one that's true, then skips the rest:
int marks = 82; if (marks >= 90) { System.out.println("Grade: A"); } else if (marks >= 75) { System.out.println("Grade: B"); } else if (marks >= 60) { System.out.println("Grade: C"); } else { System.out.println("Grade: F"); }
Order matters here. Because Java stops at the first true condition, you go from the highest bar downwards. With marks = 82, the first check (>= 90) is false, the second (>= 75) is true → it prints "Grade: B" and skips everything below.
Combining conditions
Use the logical operators && (and) and || (or) to test more than one thing at once:
int age = 25; boolean hasTicket = true; if (age >= 18 && hasTicket) { System.out.println("Welcome to the show!"); }
The switch statement
When you're checking one variable against many specific, fixed values, a long else if chain gets clunky. switch is cleaner for that:
int day = 3; switch (day) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; default: System.out.println("Another day"); }
Java jumps straight to the matching case, runs it, and break exits the switch. default is the "none of the above" catch-all (like a final else).
break! If you leave out break, Java keeps running the next cases too (this is called "fall-through"). Miss it and case 1 would also print Tuesday and Wednesday. When your switch behaves strangely, a missing break is almost always why.The modern switch (Java 14+)
Newer Java has a tidier arrow form that needs no break — handy to recognise when you see it:
switch (day) {
case 1 -> System.out.println("Monday");
case 2 -> System.out.println("Tuesday");
default -> System.out.println("Another day");
}
if/else or switch — which to use?
- Use
if / else iffor ranges and complex conditions (marks >= 75, combining with&&). - Use
switchwhen checking one variable against several exact values (a day number, a menu choice, a single character).
A missing break caused one of my most baffling early bugs — a menu that ran three options at once. Now it's the very first thing I check whenever a switch misbehaves. File that tip away; it'll save you real time one day.
Key takeaways
ifruns a block only when its (boolean) condition is true;elsehandles the "otherwise."- Chain
else iffor multiple options — Java runs the first true one, so order from strictest downwards. switchis cleaner for matching one variable to several exact values — but never forgetbreak.- Combine conditions with
&&and||.
← Previous: Lesson 6 — Input and Output in Java
Next: Lesson 8 — Loops: for, while & do-while →