Java Switch Case - Syntax, Examples, break and default

⏱️ 8 min read • Beginner Level • Lesson 25

Lesson 25 of 124 of Java Tutorial
You have completed this lesson

Completed on . You can revise this lesson or continue to the next topic.

The switch case statement in Java is used when we want to choose one block of code from multiple possible options. It is often cleaner than writing many if-else-if conditions for fixed values.

Before learning switch, you should understand Java if statement, Java if-else statement, and Java if-else-if ladder.


What is Switch Case in Java?

A switch statement allows a program to compare one expression against multiple constant values. When a matching case is found, Java executes the statements written inside that case.

  • switch is useful when there are multiple fixed choices.

  • Each choice is written using a case label.

  • The default block runs when no case matches.

Supported Data Types in switch

In Java, the switch expression supports several data types.

Supported Type Example
byte, short, char, int switch(choice)
String switch(month)
enum switch(day)
Wrapper classes Byte, Short, Character, Integer
Note: Java switch does not support float, double, boolean, or long as switch expressions.

Syntax of switch case in Java

The general syntax of a switch statement is:

SwitchSyntax.java
Copy Download
switch (expression) {
    case value1:
        // statements
        break;

    case value2:
        // statements
        break;

    default:
        // statements if no case matches
}

Flowchart of switch case

The following diagram shows how a switch statement checks case values and executes the matching case.

Switch case flowchart in Java

How does it work?

  • Java evaluates the expression inside switch.
  • The result is compared with each case value.
  • If a matching case is found, that case block starts executing.
  • The break statement stops the switch block.
  • If no case matches, the default block executes, if present.

Real-World Uses of switch Case

  • ATM menu selection.
  • Restaurant ordering systems.
  • Mobile application menus.
  • Game difficulty selection.
  • Command-based applications.

The following program shows a simple menu and performs an action based on the user's choice.

SwitchMenuExample.java
Copy Try Download
import java.util.Scanner;

public class SwitchMenuExample {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.println("MENU");
        System.out.println("----------");
        System.out.println("1. for Hello");
        System.out.println("2. for Bye");
        System.out.println("----------");

        System.out.print("Enter Your Choice: ");
        int choice = sc.nextInt();

        switch (choice) {
            case 1:
                System.out.println("HELLO USER!!");
                break;

            case 2:
                System.out.println("BYE USER!!");
                break;

            default:
                System.out.println("INVALID!!");
        }

        sc.close();
    }
}

Sample Input:

1

Output:

MENU
----------
1. for Hello
2. for Bye
----------
Enter Your Choice: HELLO USER!!

Explanation

  1. The program displays a menu to the user.
  2. The user enters a choice, and it is stored in choice.
  3. The switch statement compares choice with available cases.
  4. If choice is 1, it prints HELLO USER!!.
  5. If choice is 2, it prints BYE USER!!.
  6. If no case matches, the default block prints INVALID!!.

break Statement in switch

The break statement stops the execution of a switch block. Without break, Java continues executing the next cases. This is called fall-through.

default Case in switch

The default block executes when none of the case values match the switch expression. It is optional, but recommended when you want to handle invalid choices.

Fall-through in switch

Fall-through happens when a case does not contain break. In that case, Java continues executing the next case statements even if they do not match.

FallThroughExample.java
Copy Try Download
public class FallThroughExample {
    public static void main(String[] args) {
        int choice = 1;

        switch (choice) {
            case 1:
                System.out.println("Case 1");

            case 2:
                System.out.println("Case 2");

            default:
                System.out.println("Default case");
        }
    }
}

Output:

Case 1
Case 2
Default case
Important: Use break after each case if you do not want fall-through behavior.

Fixed Example: Using break to Prevent Fall-through

To prevent fall-through, add a break statement at the end of each case block. Once Java executes a matching case, the break statement stops the switch block immediately.

SwitchWithBreakExample.java
Copy Try Download
public class SwitchWithBreakExample {
    public static void main(String[] args) {
        int choice = 1;

        switch (choice) {
            case 1:
                System.out.println("Case 1");
                break;

            case 2:
                System.out.println("Case 2");
                break;

            default:
                System.out.println("Default");
        }
    }
}

Output:

Case 1

Explanation

  • The value of choice is 1.
  • Java matches case 1 and prints Case 1.
  • After printing, the break statement executes.
  • The break statement immediately stops the switch block.
  • Because of break, Java does not execute case 2 or the default block.
Remember: Adding break prevents Java from executing the next cases. This helps avoid accidental fall-through behavior in a switch statement.

Using String in switch

From Java SE 7, you can use a String value in a switch statement. This is useful for comparing fixed text values such as month names, commands, or menu options.

StringSwitchDemo.java
Copy Try Download
public class StringSwitchDemo {
    public static void main(String[] args) {
        String month = "august";

        switch (month) {
            case "january":
                System.out.println("1st Month");
                break;

            case "february":
                System.out.println("2nd Month");
                break;

            case "march":
                System.out.println("3rd Month");
                break;

            case "april":
                System.out.println("4th Month");
                break;

            case "may":
                System.out.println("5th Month");
                break;

            case "june":
                System.out.println("6th Month");
                break;

            case "july":
                System.out.println("7th Month");
                break;

            case "august":
                System.out.println("8th Month");
                break;

            case "september":
                System.out.println("9th Month");
                break;

            case "october":
                System.out.println("10th Month");
                break;

            case "november":
                System.out.println("11th Month");
                break;

            case "december":
                System.out.println("12th Month");
                break;

            default:
                System.out.println("Wrong Month");
        }
    }
}

Output:

8th Month

Nested switch Statement in Java

A nested switch means writing one switch statement inside another switch statement. It is used when a decision has multiple levels.

NestedSwitchExample.java
Copy Try Download
public class NestedSwitchExample {
    public static void main(String[] args) {
        int category = 2;
        int item = 1;

        switch (category) {
            case 1:
                switch (item) {
                    case 1:
                        System.out.println("Category 1, Item 1");
                        break;
                    case 2:
                        System.out.println("Category 1, Item 2");
                        break;
                    default:
                        System.out.println("Invalid Item for Category 1");
                }
                break;

            case 2:
                switch (item) {
                    case 1:
                        System.out.println("Category 2, Item 1");
                        break;
                    case 2:
                        System.out.println("Category 2, Item 2");
                        break;
                    default:
                        System.out.println("Invalid Item for Category 2");
                }
                break;

            default:
                System.out.println("Invalid Category");
        }
    }
}

Output:

Category 2, Item 1

Difference Between switch and if-else-if

switch Statement if-else-if Ladder
Best for fixed values. Best for ranges and complex conditions.
Compares one expression with multiple cases. Can check different expressions and conditions.
Uses case, break, and default. Uses if, else if, and else.
Cannot directly check conditions like x > 10 in classic switch cases. Can check relational and logical conditions easily.

Common Mistakes in switch case

  • Forgetting break, causing fall-through.
  • Using unsupported data types such as double or boolean.
  • Writing duplicate case values.
  • Expecting default to be mandatory. It is optional but useful.
  • Using switch for range-based conditions where if-else-if is better.
Summary:
  • The switch statement is used to select one block from multiple choices.
  • Each option is written using a case label.
  • The break statement prevents fall-through.
  • The default block executes when no case matches.
  • Java supports String in switch from Java SE 7.
  • Nested switch is possible but should be used carefully to keep code readable.

Interview Questions ⭐

Switch case in Java is a decision-making statement used to select one block of code from multiple choices based on the value of an expression.

The break statement stops the execution of the switch block. Without break, Java continues executing the next cases, which is called fall-through.

No, the default case is optional. It is used to execute code when none of the case values match.

Java switch supports byte, short, char, int, enum, String, and wrapper classes such as Byte, Short, Character, and Integer.

Yes, String can be used in a switch statement from Java SE 7.

Fall-through happens when a case does not contain break, causing Java to continue executing the next case statements.

Nested switch means writing one switch statement inside another switch statement.

Next step: Learn Java While Loop

🚀 Continue to Java While Loop →

🧠 Test your understanding with a quick quiz



🚀 Quick Knowledge Check

Topic: Switch-case | Language: Java

Question 1 of 10
Q1. What happens if break is missing in a matched case?
Q2. From which Java version can String be used in switch?
Q3. Which block executes when no case value matches?
Q4. Is the default block mandatory in Java switch?
Q5. What is the purpose of break in switch case?
Q6. Which keyword is used to define a possible value in a switch statement?
Q7. Which of the following is NOT supported as a classic switch expression type in Java?
Q8. What is the main purpose of a switch statement in Java?
Q9. Which statement is true about nested switch?
Q10. What will be the output of this code?
int choice = 1;
switch (choice) {
    case 1:
        System.out.println("One");
    case 2:
        System.out.println("Two");
    default:
        System.out.println("Default");
}

🎉 Great job! Continue learning Java step by step.

Discussion

Ask questions, share suggestions, or discuss this lesson.

Please login or create an account to join the discussion and save your learning activity.

Loading comments...

Have you completed this lesson?

Mark this lesson as completed to track your learning progress. To keep track across devices, please login and save your learning progress.

Not completed yet.