Switch Statement
Selects one of many code blocks to be executed.
int day = 4;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
default:
System.out.println("Weekend");
}
Important Points
- The
break keyword stops execution. If omitted, it "falls through" to the next case.
- The
default case runs if no match is found.
- Supports primitives (byte, short, int, char), Enums, and String.