Switch multi-choice structure of Java process control, theory + source code

Posted by james19 on Sun, 02 Jan 2022 15:24:04 +0100

   Hello everyone, let me share Java In process control Switch Multiple selection structure

Look at my previous blog, you will know that the previous if judgment deals with interval problems,
The Switch multi selection structure we are going to talk about today deals with the same amount of problems

This is the Switch syntax:

Write an example directly below

package struct;

public class SwitchDemo01 {
    public static void main(String[] args) {
        //case penetration matches a specific value
        char grade = 'C';
        switch (grade) {
            case 'A':
                System.out.println("excellent");
                break;    //Every time you write one, write break as much as possible. Otherwise, he will put every output
            case 'B':
                System.out.println("good");
                break;
            case 'C':
                System.out.println("pass");
                break;
            case 'E':
                System.out.println("make persistent efforts");
                break;
            case 'D':
                System.out.println("fail");
                break;
            default:
                System.out.println("Unknown level");
        }
    }
}

Here's the actual code

package struct;

public class SwitchDemo01 {
    public static void main(String[] args) {
        //case penetration matches a specific value
        char grade = 'C';      //Define the initial value variable grade=C
        switch (grade) {         
            case 'A':          //Compared with A, C is not A, so it is not A result 
                System.out.println("excellent");        //Output excellent on the console
                break;    //Every time you write one, write break as much as possible. Otherwise, he will put every output
            case 'B':          //Compared with A, B is not A, so it is not A result 
                System.out.println("good");     
                break;
            case 'C':       //Compared with C, C is C, so it is the result
                System.out.println("pass");
                break;     //end
            case 'E':         //Compared with A, E is not A, so it is not A result 
                System.out.println("make persistent efforts");
                break;
            case 'D':     //Compared with A, D is not A, so it is not A result 
                System.out.println("fail");
                break;
            default:       //If all the above conditions cannot be equal to the original value
                System.out.println("Unknown level");      //Console output content "unknown level"
        }
    }
}

See the operation results:

The following shows the results without writing break

The code is as follows:

package struct;

public class SwitchDemo01 {
    public static void main(String[] args) {
        //case penetration matches a specific value
        char grade = 'C';
        switch (grade) {
            case 'A':
                System.out.println("excellent");
                    //Every time you write one, write break as much as possible. Otherwise, he will put every output
            case 'B':
                System.out.println("good");

            case 'C':
                System.out.println("pass");

            case 'E':
                System.out.println("make persistent efforts");

            case 'D':
                System.out.println("fail");
                
            default:
                System.out.println("Unknown level");
        }
    }
}

The operation results are as follows:


Therefore, be sure to add break / / this is a good habit!

Writing a case:

public class SwitchDemo02 {
    public static void main(String[] args) {
        //The result of jdk7's new feature expression can be a string
        //The essence of characters is still numbers


        //Decompile Java ========== class (bytecode file) ------- decompile (idea)

        String name = "handsome guy";    //Take the string data type and the user-defined variable as name, and assign him "handsome"
        switch (name) {            //Compare the assignment of the variable name
            case "handsome guy":             //Compare handsome guy with handsome guy. The two values are equal
                System.out.println("handsome guy");    //Output message "handsome"
                break;          //end
            case "cute guy":       //Compared with the handsome guy, the two values are not equal, so they can't
                System.out.println("cute guy");         
                break;
            default:      //If all the above conditions cannot be equal to the original value
                System.out.println("hello");      //Console output hello

Operation:

Here is a demonstration of how default matches the input

Look at the code:

package struct;

public class SwitchDemo02 {
    public static void main(String[] args) {
        //The result of jdk7's new feature expression can be a string
        //The essence of characters is still numbers


        //Decompile Java ========== class (bytecode file) ------- decompile (idea)

        String name = "sister";
        switch (name) {
            case "handsome guy":
                System.out.println("handsome guy");
                break;
            case "cute guy":
                System.out.println("cute guy");
                break;
            default:
                System.out.println("hello");        //Neither of the above two cases is equal to the initial variable value, so the final console output is hello after default
               break;
        }
    }
}

The results are as follows:

Operation process of Switch statement:

First, define the variable and assign a value to it, 'and then the program takes this value and compares it with the constant after each case. If the two are consistent, the corresponding information after the case will be output. If they are different, the next one will be compared. If there is really no value, the corresponding information after default will be output

About Java Switch statements
I'm giving some advice:
1. default, case, Switch and break here are Java keywords
2. Switch: it means switch. This switch is the value of the expression in parentheses after the switch keyword, jdk1 After 7, the parentheses behind the switch statement can be int short byte char enumeration type and string type expression

2. Case: case means "situation, situation". It can be followed by int short byte char enumeration type and string type. It is usually a fixed value. There can be many case blocks and the order can be changed, but the constant value behind each case must be different.

3. It means "default", which means that other conditions are not met. Default must be followed by a colon. The sequence of default and case blocks can be changed without affecting the result of program execution. Generally, the default block can be placed at the end or omitted
break: means "stop", that is, jump out of the current structure
Well, that's all for the Java Switch selection structure. Thank you

Topics: Java Back-end switch