Switch

public class Main {
    public static void main(String[] args) {
        weekday(1);
        weekday(2);
        weekday(3);
        weekday(4);
        weekday(5);
        weekday(6);
        weekday(7);
    }
    public static void weekday(int day) {
        switch (day) {
            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("Invalid day");
        }
    }
}

The snippet result is

Monday

Tuesday

Wednesday

Thursday

Friday

Saturday

Sunday

You can use the switch to choose from many conditions. The keyword case is a choice. The keyword break means to jump out of the code. The keyword default means other conditions.

The weekday is a method; you could call a method to do the same function. The keyword static means it belongs to the Main class. The variable day is called parameter, while "1" means argument.

Last updated