0Pricing
Java Academy · Lesson

Switch Statements

Use switch to select among many options. Learn case labels, break, default, valid types, fall-through, and modern arrow switch.

Why Switch

Why switch? When many if/else branches test the same variable, switch is clearer and faster to scan.

Classic Syntax

Classic syntax

Use break to stop after a case. default runs when no case matches.

public class Main {
    public static void main(String[] args) {
        int n = 2;

        switch (n) {
            case 1:
                System.out.println("one");
                break;
            case 2:
                System.out.println("two");
                break;
            default:
                System.out.println("other");
        }
    }
}

All lessons in this course

  1. Comparison & Logical Operators Deep Dive
  2. Switch Statements
  3. Mini Project: Console Calculator
← Back to Java Academy