DEV Community

Cover image for What Is Switch Statement
luthfisauqi17
luthfisauqi17

Posted on

3 1

What Is Switch Statement

Switch Statement is a feature in many programming languages that allow the programmer to eliminate numerous nested if-else constructs thus improving code clarity.

Take a look on the following example:

public String toDayStringUsingIf(int dayIndex) {
        String result;

        if (dayIndex == 0) {
            result = "Sunday";
        } else if (dayIndex == 1) {
            result = "Monday";
        } else if (dayIndex == 2) {
            result = "Tuesday";
        } else if (dayIndex == 3) {
            result = "Wednesday";
        } else if (dayIndex == 4) {
            result = "Thursday";
        } else if (dayIndex == 5) {
            result = "Friday";
        } else if (dayIndex == 6) {
            result = "Saturday";
        } else {
            throw new IllegalArgumentException("Invalid day index");
        }

        return result;
    }
Enter fullscreen mode Exit fullscreen mode

In the switch statement form:

public String toDayStringUsingSwitch(int dayIndex) {
        String result;

        switch (dayIndex) {
            case 1:
                result = "Sunday";
                break;
            case 2:
                result = "Monday";
                break;
            case 3:
                result = "Tuesday";
                break;
            case 4:
                result = "Wednesday";
                break;
            case 5:
                result = "Thursday";
                break;
            case 6:
                result = "Friday";
                break;
            case 7:
                result = "Saturday";
                break;
            default:
                throw new IllegalArgumentException("Invalid day index");
        }

        return result;
    }
Enter fullscreen mode Exit fullscreen mode

You can see individual cases more clearly in the switch statement, it makes other developer understand the code easily. It may still looks longer than the if-else statement, but there are some improvement in the enhanced version of switch statement that makes the code more leaner.. Even that Switch statement is more clearer than the if-else statement, there are several conditions that Switch statement cannot replace the if-else statement.

To learn more about the enhanced version of switch in Java, you could check this article: Enhanced Switch Statement in Java

To understand this concept even more, it is highly suggested to surf throughout the reference list down below.


References:

Image cover
https://i.picsum.photos/id/42/1920/720.jpg?hmac=Cx0R9ISRIt0e1aHq11irofoe6qabiOl5Bpf668nqsiQ

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay