Switch Statement in Java

ZoneTechPro
By -
0

 

Switch Statement in Java


Java - Switch Statement
Java - Switch Statement


Introduction:

A switch statement simplifies decision-making by executing a block of code based on a single expression. Unlike if...else, it makes the code more structured and readable.


1. Switch Statement

A switch statement evaluates an expression and matches it with different cases. If no match is found, the default case runs. It supports primitive types (byte, short, char, int), String, and some wrapper classes (Character, Byte, Short, Integer).

Syntax:

switch (expression) {

    case value1:

        // code block

        break;

    case value2:

        // code block

        break;

    default:

        // default block

}



Example 1: Matching Case :


class Main {

    public static void main(String[] args) {

        String numString;

        switch (100 / 10) {

            case 10:

                numString = "Ten";

                break;

            case 20:

                numString = "Twenty";

                break;

            default:

                numString = "Other Number";

                break;

        }

        System.out.println(numString);

    }

}

Output: 

Ten

Here, 100 / 10 = 10, so case 10 executes.

Example 2: No Matching Case :


class Main {
    public static void main(String[] args) {
        String numString;

        switch (100 / 4) {
            case 10:
                numString = "Ten";
                break;
            default:
                numString = "Other Number";
                break;
            case 20:
                numString = "Twenty";
                break;
        }
        System.out.println(numString);
    }
}

Output:


Other Number

Since 100 / 4 = 25 doesn’t match any case, the default case runs.


Example 3: Optional Default Case :


class Main {
    public static void main(String[] args) {
        int remainder = 4 % 3;
        switch (remainder) {
            case 0:
                System.out.println("Even Number");
                break;
            case 1:
                System.out.println("Odd Number");
                break;
        }
    }
}


Output :

Odd Number

Here, 4 % 3 = 1, so case 1 executes. The default case is optional.

2. Break in Switch-Case :

The break statement stops execution after a matching case. If omitted, execution falls through and continues with subsequent cases.


Example 1: Missing Break (Fall-through Behavior) :


class Main {
    public static void main(String[] args) {
        int result = 3 + 1;
        switch (result) {
            case 4:
                System.out.println("First Case");
            case 8:
                System.out.println("Second Case");
            case 12:
                System.out.println("Third Case");
                break;
            default:
                System.out.println("Default Case");
        }
    }
}

Output:

First Case
Second Case
Third Case


Since break is missing, execution continues through all cases.

Example 2: No Break in Any Case :


class Main {
    public static void main(String[] args) {
        int result = 2 * 2;
        switch (result) {
            case 2:
                System.out.println("First Case");
            case 4:
                System.out.println("Second Case");
            case 6:
                System.out.println("Third Case");
            default:
                System.out.println("Default Case");
        }
    }
}

Output:

Second Case
Third Case
Default Case

Here, 2 * 2 = 4, so case 4 executes. Since break is missing, execution continues to case 6 and default case.


Summary

  • Switch statements evaluate an expression and match it with predefined cases.
  • Work with primitive types, String, and some wrapper classes.
  • Break statements prevent fall-through execution. If missing, all subsequent cases run.
Tags:

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Learn more
Ok, Go it!