On switch statement

Posted by shae marks on Wed, 18 Dec 2019 00:14:49 +0100

Format of switch statement:

Switch (expression)

case value 1: execute statement; break;

case value 2: execute statement; break;

           ...

default: execute statement; break;

 

switch statement features:

(1) the value of expression can be byte, short, int, char;

After jdk5, enumeration types can be used;

After jdk7, String can be used;

(2) the case is followed by the value to be compared with the expression, and the value can only be a constant, not a variable, and the values of all case items are of the same type;

(3) the default item is optional, and the default item does not have to be placed at the end

 

matters needing attention:

break statement is not necessary;

If there is no break statement, there will be a "penetration phenomenon". Here is a simple example:

The user inputs an integer between 1 and 5, and the system outputs the number.

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        System.out.println("please input a number:");
        int a=input.nextInt();
        switch (a){
            case 1:
                System.out.println("1");
                break;
            case 2:
                System.out.println("2");
                break;
            case 3:
                System.out.println("3");
                break;
            case 4:
                System.out.println("4");
                break;
            case 5:
                System.out.println("5");
                break;
            default:
                System.out.println("No such number");
                break;
        }
    }
}

If there is no break, the output is as follows:

Match to 2, and then output 2. It will run "through" and run the default statement all the time.

Removing the break after the case statement is not all bad. Take the following example:

Assuming that December, January and February are spring and winter, March to May are spring, June to August are summer, September to November are autumn, the customer enters an integer, and the system returns the season.

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        System.out.println("please input a number:");
        int num=input.nextInt();
        switch (num){
            case 12:
            case 1:
            case 2:
                System.out.println("Winter!");
                break;
            case 3:
            case 4:
            case 5:
                System.out.println("Spring!");
                break;
            case 6:
            case 7:
            case 8:
                System.out.println("Summer!");
                break;
            case 9:
            case 10:
            case 11:
                System.out.println("Autumn!");
                break;
            default:
                System.out.println("There is no corresponding season!");

        }
    }
}

 

Using the break after the case statement can solve this problem easily.  

Topics: Spring Java