Java Switch Statement
gocourse.in Maintenance

We'll be back soon

Our CDN (cdn.gocourse.in) is currently unreachable. Some images, JavaScript, or CSS files may not load properly.

Estimated downtime: ~30 minutes

Java Switch Statement

Jeevadharshan

Java Switch Statement

The Java switch statement is a multi-branch control structure that lets you execute one block of code among many based on the value of an expression. It’s often cleaner and more readable than long chains of if–else statements—especially when comparing a single variable against multiple constant values.  

What Is a Switch Statement in Java?  

A switch evaluates an expression once and matches its result against predefined case values. When a match is found, the corresponding code block runs. 
 
Syntax 
 
switch (expression) { 
    case value1: 
        // code block 
        break; 
    case value2: 
        // code block 
        break; 
    default: 
        // code block 
}

How Java Switch Works 

  • The expression is evaluated. 
  • Java compares the result with each case. 
  • When a match occurs, that block executes. 
  • break stops further execution. 
  • default runs if no case matches.
Example 1: Grade Evaluation Program  
 
This program converts a numeric score into a letter grade using a switch statement. 
 
public class GradeSwitchExample { 
    public static void main(String[] args) { 
        int score = 82; 
        char gradeCategory; 
 
        if (score >= 90) { 
            gradeCategory = 'A'; 
        } else if (score >= 75) { 
            gradeCategory = 'B'; 
        } else if (score >= 60) { 
            gradeCategory = 'C'; 
        } else { 
            gradeCategory = 'F'; 
        }

switch (gradeCategory) { 
            case 'A': 
                System.out.println("Excellent performance"); 
                break; 
            case 'B': 
                System.out.println("Good job"); 
                break; 
            case 'C': 
                System.out.println("Satisfactory"); 
                break; 
            case 'F': 
                System.out.println("Needs improvement"); 
                break; 
            default: 
                System.out.println("Invalid grade"); 
        } 
    } 
}  

Output: 

Good job

The break Keyword in Switch 

The break statement terminates the switch after a matching case executes. Without break, Java continues executing the next cases—this is called fall-through. 
 
Example 2: Demonstrating Fall-Through 
 
public class SwitchFallThroughDemo { 
    public static void main(String[] args) { 
        int level = 2; 
 
        switch (level) { 
            case 1: 
                System.out.println("Beginner"); 
            case 2: 
                System.out.println("Intermediate"); 
            case 3: 
                System.out.println("Advanced"); 
            default: 
                System.out.println("Level selection complete"); 
        } 
    } 
}  

Output: 

Intermediate 
Advanced 
Level selection complete 
 
Because no break statements are used, execution continues after the matched case.

The default Case in Java Switch 

The default block runs when none of the case values match the expression. 
 
Example 3: Menu Selection System  
 
public class MenuSwitchExample { 
    public static void main(String[] args) { 
        int menuChoice = 5; 
 
        switch (menuChoice) { 
            case 1: 
                System.out.println("Opening Dashboard"); 
                break; 
            case 2: 
                System.out.println("Loading Profile"); 
                break; 
            case 3: 
                System.out.println("Showing Settings"); 
                break; 
            case 4: 
                System.out.println("Logging out"); 
                break; 
            default: 
                System.out.println("Invalid option selected"); 
        } 
    } 

Output: 

Invalid option selected

When to Use Switch Instead of If–Else 

Use a switch when:
  • You compare one variable against many constant values 
  • Conditions are equality-based (==) 
  • You want cleaner, structured branching 
  • Performance matters in large decision sets 
Use if–else when:
  • Conditions involve ranges (>, <, <=) 
  • Multiple variables are involved 
  • Logical operators (&&, ||) are needed

Modern Java Switch (Java 14+) 

Newer Java versions support a concise switch expression: 
 
public class ModernSwitchExample { 
    public static void main(String[] args) { 
        int day = 3; 
 
        String type = switch (day) { 
            case 1, 7 -> "Weekend"; 
            case 2, 3, 4, 5, 6 -> "Weekday"; 
            default -> "Invalid"; 
        }; 
 
        System.out.println(type); 
    } 
} 

Tags
Our website uses cookies to enhance your experience. Learn More
Accept !