codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

14 / 65 topics
1Java Intro2Java Get Started3Java Syntax4Java Output5Java Comments6Java Variables7Java Data Types8Java Type Casting9Java Operators10Java Strings11Java Math12Java Booleans13Java If...Else14Java Switch15Java While Loop16Java For Loop17Java Break/Continue18Java Arrays
Tutorials/Java Programming/Java Switch
☕Java Programming

Java Switch

Updated 2026-05-12
25 min read

Java Switch

In this tutorial, you'll learn about the switch statement in Java, which is a control flow statement that allows you to execute different blocks of code based on the value of a variable. The switch statement is particularly useful when you have multiple conditions to check against a single variable. This topic builds upon your understanding of conditional statements from the previous "Java If...Else" tutorial.

Introduction

The switch statement in Java provides an alternative to using multiple if-else statements, especially when dealing with a large number of discrete cases. It can make your code more readable and maintainable by grouping related conditions together. Understanding how to use the switch statement effectively is crucial for writing clean and efficient Java programs.

Core Content

Basic Syntax

The basic syntax of a switch statement in Java is as follows:

Java
1switch (expression) {
2 case value1:
3 // code block
4 break;
5 case value2:
6 // code block
7 break;
8 default:
9 // code block
10};
  • expression: This can be any variable of a primitive data type (char, byte, short, int) or an enumeration.
  • case value1, case value2: These are the possible values that the expression might match. If the expression matches one of these values, the corresponding block of code is executed.
  • break: This keyword is used to exit the switch block once a matching case has been executed. Without break, the program will continue executing the code in subsequent cases (a behavior known as "fall-through").
  • default: This optional block specifies what should happen if none of the cases match the expression's value.

Example 1: Basic Switch Statement

Let's start with a simple example to demonstrate how the switch statement works. We'll create a program that prints the name of the day based on an integer input representing the day number (1 for Monday, 2 for Tuesday, etc.).

DaySwitch.java
1public class DaySwitch {
2 public static void main(String[] args) {
3 int day = 3;
4
5 switch (day) {
6 case 1:
7 System.out.println("Monday");
8 break;
9 case 2:
10 System.out.println("Tuesday");
11 break;
12 case 3:
13 System.out.println("Wednesday");
14 break;
15 case 4:
16 System.out.println("Thursday");
17 break;
18 case 5:
19 System.out.println("Friday");
20 break;
21 case 6:
22 System.out.println("Saturday");
23 break;
24 case 7:
25 System.out.println("Sunday");
26 break;
27 default:
28 System.out.println("Invalid day number");
29 }
30 }
31}
Output
Wednesday

In this example, the variable day is set to 3. The switch statement checks the value of day and executes the corresponding case block for Wednesday.

Example 2: Using Fall-Through

If you omit the break keyword, the program will continue executing the code in subsequent cases (fall-through). This can be useful if multiple cases share the same behavior. However, it's important to use fall-through carefully to avoid unintended side effects.

FallThroughSwitch.java
1public class FallThroughSwitch {
2 public static void main(String[] args) {
3 int number = 2;
4
5 switch (number) {
6 case 1:
7 System.out.println("One");
8 case 2:
9 System.out.println("Two");
10 case 3:
11 System.out.println("Three");
12 default:
13 System.out.println("Other");
14 }
15 }
16}
Output
Two
Three
Other

In this example, the output includes "Two", "Three", and "Other" because there are no break statements after the first two cases. The program falls through to subsequent cases until it encounters a break or reaches the end of the switch block.

Example 3: Using Default Case

The default case in a switch statement acts as an "else" clause for all other cases that are not explicitly matched. It's important to include a default case to handle unexpected values gracefully.

DefaultSwitch.java
1public class DefaultSwitch {
2 public static void main(String[] args) {
3 String fruit = "banana";
4
5 switch (fruit.toLowerCase()) {
6 case "apple":
7 System.out.println("Apple is a fruit.");
8 break;
9 case "orange":
10 System.out.println("Orange is a citrus fruit.");
11 break;
12 default:
13 System.out.println("Unknown fruit.");
14 }
15 }
16}
Output
Unknown fruit.

In this example, the variable fruit is set to "banana". Since there's no case for "banana", the program executes the code in the default block, printing "Unknown fruit."

Common Mistakes

  1. Forgetting Break Statements: One of the most common mistakes when using switch statements is forgetting to include break statements. This can lead to unexpected behavior known as fall-through.
  2. Using Non-Primitive Types: The expression in a switch statement must be of a primitive type or an enumeration. Using objects (like Strings) directly without proper handling can lead to errors.
  3. Overlapping Cases: Ensure that your cases do not overlap unintentionally, especially if you're using fall-through.

Best Practices

  • Use break Statements: Always include break statements unless intentional fall-through is required.
  • Keep It Simple: Avoid complex logic inside switch blocks to maintain readability.
  • Handle All Possible Cases: Consider using a default case to handle any unexpected values gracefully.

Practical Example

Let's create a practical example that demonstrates the use of the switch statement in a real-world scenario. We'll build a simple program that converts a numeric grade into a letter grade.

GradeConverter.java
1public class GradeConverter {
2 public static void main(String[] args) {
3 int grade = 85;
4
5 switch (grade / 10) {
6 case 10:
7 case 9:
8 System.out.println("Grade: A");
9 break;
10 case 8:
11 System.out.println("Grade: B");
12 break;
13 case 7:
14 System.out.println("Grade: C");
15 break;
16 case 6:
17 System.out.println("Grade: D");
18 break;
19 default:
20 System.out.println("Grade: F");
21 }
22 }
23}
Output
Grade: B

In this example, the program converts a numeric grade into a letter grade using a switch statement. The expression grade / 10 determines the range of the grade, and the corresponding case block prints the appropriate letter grade.

Summary

ConceptDescription
Switch StatementA control flow statement that executes different blocks of code based on a variable's value.
Break KeywordUsed to exit the switch block after executing a matching case.
Default CaseSpecifies what should happen if none of the cases match the expression's value.

What's Next?

Now that you've learned about the switch statement, it's time to explore loops in Java. The next topic is "Java While Loop," where we'll learn how to repeat a block of code multiple times using different types of loops.

Stay tuned for more tutorials and happy coding!


PreviousJava If...ElseNext Java While Loop

Recommended Gear

Java If...ElseJava While Loop