preface

C: Last time we introduced the if series of selective structure syntax, the overall feeling should be easy to understand and easy to write. The switch is not the same as the switch.

A series of reading

  • The Java syntax | control statements and flow chart”
  • The Java syntax | if choose structure”
  • The Java syntax | switch selection structure”

According to?

With if, why learn to select structures with switch? Let’s take a look at an example.

Case requirement: Xiao Yang participated in the creation 502 program.

  • If she wins the first prize, she will play the lead actress in Jade Queen
  • If she wins the second place, she will play the leading actress in “Doula Continent.
  • If she wins the third place, she will play the lead actress in Eternal Night

According to the requirement introduction, the condition expression of the case is a simple equivalence judgment, but there are many conditions and they are mutually exclusive, which can be realized by multiple IFs.

// Store rankings
int score = 2;

// Use multiple ifs
if (score == 1) {
    System.out.println("Xiao Yang will be the leading lady in Jade Dynasty!");
} else if (score == 2) {
    System.out.println("Xiao Yang will be the leading lady of Douluo Continent!");
} else if (score == 3) {
    System.out.println("Xiao Yang will be the heroine of Eternal Night!");
}
Copy the code

If = “if”; if = “if”; if = “if”; if = “if”;

Let’s look at the syntax:

switch(expression) {caseconstant1: statement;break;
    caseconstant2: statement;break;
        / /...
    default: statement;break;
}
Copy the code

Apply its syntax again to solve the problem just now, is it found much clearer?

// Store rankings
int score = 2;

/ / use the switch
switch (score) {
    case 1:
        System.out.println("Xiao Yang will be the leading lady in Jade Dynasty!");
        break;
    case 2:
        System.out.println("Xiao Yang will be the leading lady of Douluo Continent!");
        break;
    case 3:
        System.out.println("Xiao Yang will be the heroine of Eternal Night!");
        break;
}
Copy the code

Use attention

  1. Switch can be used for judgment only when equivalent judgment is satisfied, but not for range judgment.

  2. If there is no special requirement, you must append a break to each case.

    Break means to end a case, if there is no break, case penetration will occur, that is, continue to execute until the next break ends!

    // Store rankings
    int score = 1;
    / / use the switch
    switch(score){
        case 1:
            System.out.println("Xiao Yang will be the leading lady in Jade Dynasty!");
        case 2:
            System.out.println("Xiao Yang will be the leading lady of Douluo Continent!");
            break;
        case 3:
            System.out.println("Xiao Yang will be the heroine of Eternal Night!");
            break;
    }
    Copy the code

    In the code above, if a break is missing after case 1, the output will be.

    Xiao Yang will be the heroine of Jade Dynasty! Xiao Yang will be the leading lady of Douluo Continent!Copy the code
  3. It is recommended to add a default for default processing.

  4. Switch expressions support the following types: int (short, byte, char can be automatically converted to int), Enum (Enum), and String (since JDK1.7, Switch supports String equivalence judgment, see official release introduction).

Switch vs. If

So far, the choices in the Java structure we finished learning, although a lot of grammar theory faction only two, one is the if, one is the switch, and the switch and multiple if also alike, and more easy to understand.

Similarities: Both structures are used to handle multi-branch conditions.

Difference: Switch can only deal with the case of equivalent condition judgment, the multiple if selection structure has no restriction of switch selection structure, especially suitable for the case when a variable is in a continuous interval (range-type judgment).

In terms of efficiency, switch is faster than if, but with the development of hardware, the efficiency gap between the two can be almost ignored.

In fact, the optimization of Switch has been going on in recent generations of Java versions. The reason why switch is being used less and less now is not because I don’t want to use it, but when a programmer takes over a project of others, there are often a lot of if and else code in the project.

If you change it to Switch, it will not only fail to achieve the desired results, but also cause the framework to crash. Therefore, in order not to do Thanos, the programmers who take over the disk will add a few if and else at the end, and in a long time, the switch usage will naturally be low!

Afterword.

With the advent of choice structures, we can simulate real life business logic in programs. These flow control statements are as basic as Hanyu Pinyin and basic Chinese characters, so remember the grammar.

The article case realization, must begin to realize! Because understanding and mastery are two different things! It’s as if you’ve watched so many tutorials on life on Tiktok: folding clothes, playing guitar….. But you never practice it, you never learn.

Teacher Zha said: For the learning of technology, teacher Zha has always followed the following steps: With a simple demo to let it run first, and then learn it the most commonly used API and configuration can let yourself up, finally, on the basis of proficiency in the spare time reading the source to try to make myself to be able to see clearly its running mechanism, part of the cause of the problem, at the same time, draw lessons from these technology to enhance your own code level.

So in the teacher’s article, the early basic are small white, only interspersed with a very small amount of source research. Of course, such as the small white update, you still like, the late will not regularly dedicated to part of the technology of the source code analysis.