The switch statement
22 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)A switch statement looks at one value and jumps straight to the matching case. It replaces long ladders like "if the day is 1…, else if the day is 2…".
switch: jump to the matching case
A timetable app must turn day numbers 1 to 7 into names, and a ladder would repeat day == 1, day == 2 … seven times. A switch reads the value once and jumps to the case with that value.
- Line 1: no case matched
Example 1 · Day number to day name
import java.util.Scanner;
public class DayName { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Day number (1-7): "); int day = sc.nextInt(); 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; case 5: System.out.println("Friday"); break; case 6: System.out.println("Saturday"); break; case 7: System.out.println("Sunday"); break; default: System.out.println("Invalid day"); } sc.close(); }}Day number (1-7): 3 Wednesday
What this code does
dayis 3, so Java jumps tocase 3:, printsWednesday, andbreakends the switch.- For a number like 9 no case matches, so
defaultprintsInvalid day.
default: works like the final else of a ladder: it is optional, and it runs only when no case matches.
break and fall-through
A student orders item 2 from the canteen menu, but the bill shows Dosa, Poori and Tea. Without break, Java keeps running the cases below the match: this is called fall-through.
Example 2 · Missing break: fall-through
public class Menu { public static void main(String[] args) { int choice = 2; switch (choice) { case 1: System.out.println("Idli"); case 2: System.out.println("Dosa"); case 3: System.out.println("Poori"); default: System.out.println("Tea"); } }}Dosa Poori Tea
What this code does
- Java starts at
case 2:and printsDosa. - No case has a
break, so it falls through every case below. Addbreak;after each print to get onlyDosa.
Fall-through goes on until Java meets a break or the end of the switch. When it is not planned it is a bug, so end every case with break;; only the last one may leave it out.
Cases that share a block
Saturday and Sunday both mean "Holiday", so why write the message twice? Here fall-through helps: write the cases one after another with nothing between them, and they share a block.
Example 3 · String cases that share a block
public class Weekend { public static void main(String[] args) { String day = "Sun"; switch (day) { case "Sat": case "Sun": System.out.println("Holiday"); break; default: System.out.println("College day"); } }}Holiday
What this code does
case "Sat":has no lines of its own, so both days lead to theHolidayblock.- The
breakafter it keeps the weekend from falling intodefault.
A String case compares the text the way equals() does. switch with arrows (opens in a new tab) writes the same thing as case "Sat", "Sun" ->.
What switch can check
A menu choice, a day number or an operator sign is one value from a fixed list. So each case holds one fixed value, such as 3, '+' or "Mon", and no value may appear twice.
int day = 1;switch (day) { case 1: System.out.println("Monday"); break; case 1: // error: duplicate case label System.out.println("Again"); break;}A switch works on int, char and String values (also byte and short), not on double, float, long or boolean. It cannot hold a range such as marks >= 75; for ranges use an else-if ladder (opens in a new tab).
Next step
Check your understanding
8 quick questions