[JAVA] switch statement

The switch statement branches the part to be displayed according to the read numerical value.

filename.java


import jva.util.Scanner;

class Abc {
    publlic static void(String[] args) {
        Scanner stdin = new Scanner(System.in);

        System.out.print("integer: ");
        int n = stdIn.nextInt();

        switch (n) {
            case 0 : System.out.print("abc"); 
                     break;
            case 1 : System.out.print("def"); 
                     break;
            case 2 : System.out.print("ghi"); 
                     break;
            case 3 :
            case 4 : System.out.print("jkl");
                     break;
            default : System.out.print("123");
                      break;
        }
        System.out.println();
    }
}

In the case of the example, the displayed sentence is divided according to the value of "int n". Also, if the value of n is 3 or 4, the same character is displayed. "Default" is displayed when n is not the value specified for case.

The switch statement ends when the "break statement" at the end of each case is executed. The statement after the switch statement is executed after it is terminated by the break statement.

Recommended Posts

switch statement
Java switch statement
Switch statement range specification
Studying Java-Part 11-switch statement
[Swift] switch statement using tuples
Let's understand the switch statement!
[Java] Branch enum with switch statement
[Java] Really scary switch statement story
Loop statement
I met a great switch statement
if statement
Java, if statement / switch statement starting from beginner
Basics of java basics ② ~ if statement and switch statement ~
Java switch statement and break, Kotlin when expression ...
switch and EnumMap
Java for statement