01 / 21

Switch Statements
in Java

Master multi-way branching with switch statements and understand the critical concept of fall-through behavior.

Java

What is a Switch Statement?

A switch statement lets you test a single variable against multiple possible values and execute different code for each match. Think of it as a more organized alternative to writing a long chain of if-else if blocks.

When Should You Use Switch?

✓ Discrete Values
When you compare one variable against specific, known constants like 1, 2, 3 or "A", "B", "C".
✓ Menu Selection
Choosing actions from numbered menus, day-of-week logic, or grade-letter mapping.
✕ Ranges or Conditions
Switch cannot test ranges like \( x > 10 \) or compound boolean conditions. Use if-else for those.

Anatomy of a Switch

1 2 3 4 5 6 7 8 9 10 11 switch ( expression ) { case value1 : // statements break; case value2 : // statements break; default : // fallback statements expression to evaluate exits the switch runs if no case matches

How Switch Flows

Evaluate expression == case 1? yes Run code 1 no == case 2? yes Run code 2 no default code

Supported Data Types

The expression inside switch() must evaluate to one of these types. You cannot use floating-point numbers, booleans, or objects (except String).

int / byte / short
Integer types and their wrapper classes (Integer, Byte, Short).
char
Single characters like 'A', 'B', 'C' enclosed in single quotes.
String
Text strings (since Java 7). Case-sensitive matching.
enum
Enumerated types you define, like Season.WINTER or Day.MONDAY.

Switch vs If-Else

switch if-else if
Tests Exact value equality Any boolean condition
Readability Cleaner with many values Better for 2-3 conditions
Ranges Not supported Fully supported
Types int, char, String, enum Any expression
Fall-through Yes (unique feature) No

How to Write a Switch

1
Identify the variable. Pick the single value you want to branch on (e.g., int day).
2
Write switch(variable). Place the variable or expression inside the parentheses.
3
Add case labels. Each case value: defines a possible match. Values must be compile-time constants.
4
Write the code for each case. Put the statements to execute after the colon.
5
Add break; after each case. Without it, execution falls through to the next case.
6
Include a default: case. This catches any value not matched above.

Example: Day of the Week

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("Other day"); }

Example: Grade Converter

char grade = 'B'; switch (grade) { case 'A': System.out.println("Excellent"); break; case 'B': System.out.println("Good job"); break; case 'C': System.out.println("Average"); break; case 'F': System.out.println("Failing"); break; default: System.out.println("Invalid grade"); }

Understanding Fall-Through

Fall-through is what happens when you omit the break statement. After a matching case runs, Java does not automatically stop: it keeps executing the next case's code, and the next, until it hits a break or reaches the end of the switch block.

case 1 no break! case 2 no break! case 3 vs case 1 break; case 2 case 3 skipped entirely all three run!

Fall-Through in Action

int x = 1; switch (x) { case 1: System.out.println("One"); // no break: falls through! case 2: System.out.println("Two"); // no break: falls through! case 3: System.out.println("Three"); break; }

Intentional Fall-Through

Sometimes fall-through is useful: when multiple values should run the same code, you can stack cases together without repeating yourself.

int day = 6; switch (day) { case 1: case 2: case 3: case 4: case 5: System.out.println("Weekday"); break; case 6: case 7: System.out.println("Weekend"); break; }

The default Case

Safety Net
Catches any value not covered by your case labels, preventing silent failures.
Placement
Usually written last, but Java allows it anywhere. Last position is the convention.
String color = "purple"; switch (color) { case "red": System.out.println("Stop"); break; case "green": System.out.println("Go"); break; default: System.out.println("Unknown color"); }

Common Mistakes

✕ Forgetting break
Without break, execution falls through to the next case, producing unexpected extra output.
✕ Using Variables as Cases
Case values must be compile-time constants (literals or final variables), not regular variables.
✕ Unsupported Types
Switching on double, float, long, or boolean causes a compilation error. Stick to int, char, String, or enum.
✕ Duplicate Case Values
Two cases with the same value (e.g., two case 1:) will not compile. Each value must be unique.

Switch Cheat Sheet

Keyword Purpose Required?
switch Starts the switch block Yes
case Defines a value to match At least one
break Exits the switch after a case Recommended
default Fallback when no case matches Recommended

Question 1 of 5

What is the primary purpose of a break statement inside a switch?

Question 2 of 5

What does this code print?

Question 3 of 5

Which of the following data types cannot be used in a switch statement expression?

Question 4 of 5

What does this code print?

Question 5 of 5

When is intentional fall-through considered good practice in a switch statement?