Skip to lesson
MentorNxt home

Java Programming

Conditional Statements

The switch statement

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.

Syntax
switch (value) {
case value1: statements break;
case value2: statements break;
default: statements
}
  1. Line 1: no case matched
day is 3, so switch jumps straight to case 3 and prints Wednesday. break then ends the switch and Java goes to the next line after it. Cases 1 and 2 never run.

Example 1 · Day number to day name

DayName.javaJava
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();    }}
Sample output
Day number (1-7): 3
Wednesday

What this code does

  • day is 3, so Java jumps to case 3:, prints Wednesday, and break ends the switch.
  • For a number like 9 no case matches, so default prints Invalid 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.

Picture 1 / 3
choice is 2, so the switch jumps to case 2 and prints Dosa. Case 2 has no break.
Picture 1 / 3

Example 2 · Missing break: fall-through

Menu.javaJava
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");        }    }}
Output
Dosa
Poori
Tea

What this code does

  • Java starts at case 2: and prints Dosa.
  • No case has a break, so it falls through every case below. Add break; after each print to get only Dosa.

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

Weekend.javaJava
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");        }    }}
Output
Holiday

What this code does

  • case "Sat": has no lines of its own, so both days lead to the Holiday block.
  • The break after it keeps the weekend from falling into default.

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.

Java
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