About Us Contact Us Write for Us Advertise
Home > Java > Java if, else and switch: Making Decisions in Your Code
Java

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.

Shiv Pandey
Shiv Pandey
Sep 18, 2026 | 1 views
Java if, else and switch: Making Decisions in Your Code

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).

The #1 switch bug — don't forget 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 if for ranges and complex conditions (marks >= 75, combining with &&).
  • Use switch when 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

  • if runs a block only when its (boolean) condition is true; else handles the "otherwise."
  • Chain else if for multiple options — Java runs the first true one, so order from strictest downwards.
  • switch is cleaner for matching one variable to several exact values — but never forget break.
  • Combine conditions with && and ||.

← Previous: Lesson 6 — Input and Output in Java
Next: Lesson 8 — Loops: for, while & do-while →

Related Articles

Input and Output in Java: Reading User Input with Scanner
Java

Input and Output in Java: Reading User Input with Scanner

Operators in Java Explained (with Examples)
Java

Operators in Java Explained (with Examples)

Variables and Data Types in Java (Beginner's Guide)
Java

Variables and Data Types in Java (Beginner's Guide)

Java Syntax Basics: Your First Program Explained Line by Line
Java

Java Syntax Basics: Your First Program Explained Line by Line