switch statement in java

PROBLEM

You wish to use the switch statement in Java. Below we will see a classic example converting a day number to day name. Let’s suppose we have an integer number that represents the day of the week. The number is from 1 to 7 and 1 goes for Monday and 7 for Sunday as shown in the table below.

Description
Day number
1
2
3
4
5
6
7
Day name
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday

We need a program that given this number will print out the name of the day.

solution

The switch is mainly used when we have 3 or more conditions that we want to check. If we want to check for something only 2 different cases then a normal if-statement can do its job. In the case above will need to check 7 different numbers and decide the name of the day.

The switch statement is made up with different cases. In our case we need 7 and one default that will act as the else case. For example when a number is not in the range of 1 to 7 the default case will handle it and print a user friendly message.
Let’s see the code below that will use the switch on a int variable:
				
					package com.programmerabroad;

public class SwitchStatement {

    public static void main(String[] args) {

        int dayNumber = 3;

        switch (dayNumber) {
            case 1:
                System.out.println("Monday");
                break;
            case 2:
                System.out.println("Tuesday");
                break;
            case 3:
                System.out.println("Wednesday");
                break;
            case 4:
                System.out.println("Thursday");
                break;
            case 5:
                System.out.println("Friday");
                break;
            case 6:
                System.out.println("Saturday");
                break;
            case 7:
                System.out.println("Sunday");
                break;
            default:
                System.out.println("Unrecognized day number. Please use 1 to 7");
        }
    }
}

				
			

The break is needed in every case because it stops the code from falling in the next case or the default case. In the example above, it enters the case 3, prints the text and then breaks out of the switch statement.

output

Correct! The output is Wednesday

switch-statement-java

conclusion

In this post we saw how to use the switch statement. I suggest you try to remove the break from the switch and see if it breaks, also you can use the switch on a String variable to achieve the opposite result.

Share it!

Facebook
Twitter
LinkedIn
Reddit
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

Strictly Necessary Cookies

Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.

Google Analytics Cookies

This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.

Keeping this cookie enabled helps us to improve our website.

HotJar Cookies

We use Hotjar in order to better understand our users’ needs and to optimize this service and experience. Hotjar is a technology service that helps us better understand our users’ experience (e.g. how much time they spend on which pages, which links they choose to click, what users do and don’t like, etc.) and this enables us to build and maintain our service with user feedback. Hotjar uses cookies and other technologies to collect data on our users’ behavior and their devices. This includes a device's IP address (processed during your session and stored in a de-identified form), device screen size, device type (unique device identifiers), browser information, geographic location (country only), and the preferred language used to display our website. Hotjar stores this information on our behalf in a pseudonymized user profile. Hotjar is contractually forbidden to sell any of the data collected on our behalf.

For further details, please see the ‘about Hotjar’ section of Hotjar’s support site.