The second type of branch statement: switch statement

We have already talked about the if statement in the branch structure before. Today we will introduce the last branch statement in C language: switch. There will be some differences between this switch and the previous if, so please pay attention.

switch

This is a special form of if…else structure, which is used to determine when a condition has multiple results. It transforms the previous else if into a simpler, easier to use, and more readable form.

switch(expression)
{
    case value1:statement
    case value2:statement
    default:statement
}

In the above code, the corresponding case branch is executed according to the different values of the expression in the parentheses after the switch, and value1 and value2 represent integer constant expressions. If the expression result in the parentheses after the switch is not the one after the case If the integer constant expression is an integer constant expression, the default branch will be executed.

That is to say, if the expression result in switch() can correspond to the value after the case, it will be executed from that case one by one until default, and then exit.

For example: When we use mobile phone chargers, we generally prefer to use the charger that comes with the mobile phone, because the one used can correspond to the mobile phone socket. As long as you use the original charger, you will keep using it until you can’t find it anymore, and then use it. Replacement charger, so default is a replacement here. case and default will only execute one of them.

Highlights! ! !

The expression after switch must be an integer expression, such as 2 + 1, 5-2, ‘b’-‘a’, ‘9’-‘7’,

The value after case must be an integer constant expression, such as 5, 4

There may be questions here, why English can also be integer expressions. And the numbers are also added with single quotes, because characters are used here. After adding single quotes (”) to a letter or number, it becomes a character, and the data of characters in the computer is stored in ASCII encoding. If If necessary, you can move to our first section.

https://blog.csdn.net/zjajaaa/article/details/133952058

So when there is an operation between characters, it is an operation between ASCII codes, that is, the operation expression between characters is also an integer expression.

Comparison between if statement and switch statement

Exercise: Enter any integer value and calculate the remainder after dividing by 3.

If done using an if statement, as follows:

#include <stdio.h>
int main()
{
    int n = 0;
    scanf("%d", & amp;n);
    if(n%3 == 0)
        printf("divisible, remainder is 0\\
");
    else if(n%3 == 1)
        printf("The remainder is 1\\
");
    else
        printf("The remainder is 2\\
");
    return 0;
}

If you use a switch statement to rewrite it, it can look like this:

#include <stdio.h>
int main()
{
    int n = 0;
    scanf("%d", & amp;n);
    switch(n%3)
    {
    case 0:
        printf("divisible, remainder is 0\\
");
        break;
    case 1:
        printf("The remainder is 1\\
");
        break;
    case 2:
        printf("The remainder is 2\\
");
        break;
    }
    return 0;
}

Notice:

There must be a space between case and the following number, for example: case 0

After the code in each case statement is executed, a break needs to be added to jump out of the switch.

In the above code, if n=3, then 3%3 is 0, and then outputs “divisible, remainder is 0” under case 0. Then due to break, the current code ends early, otherwise it will be executed to the end.

We can introduce it in detail again, what will happen if there is no break?

break in switch statement

For example:

#include <stdio.h>
int main()
{
    int n = 0;
    scanf("%d", & amp;n);
    switch(n%3)
    {
    case 0:
        printf("divisible, remainder is 0\\
");
    case 1:
        printf("The remainder is 1\\
");
    case 2:
        printf("The remainder is 2\\
");
    }
    return 0;
}

result:

3

Divisible, remainder is 0

remainder is 1

The remainder is 2

When we enter 3, the result of 3%3 is 0, and then the first case is executed first, and then the execution continues one by one until the end. So it can be seen that the switch statement also has a branching effect. When we want to exit the statement during the execution of the switch, we need to add a break after the case we want to stop, just like the two strings of code above.

So break in the switch statement is very important. Can achieve real branching effect.

Does every switch need to use break? This depends on the actual situation.

For example:

practise:

Enter a number from 1 to 7 and print the corresponding day of the week?

For example:

Input?: 1 Output: Day of the week?

Input?: 2 Output: Day of the week?

Loss?: 3 Output: Wednesday

Loss?: 4 Output: Thursday

Loss?: 5 Output: Friday

Loss?: 6 Output: Saturday

Lose?: 7 Output: Sunday

#include <stdio.h>
int main()
{
    int day = 0;
    scanf("%d", & amp;day);
    switch(day)
    {
    case 1:
        printf("Monday\\
");
        break;
    case 2:
        printf("Tuesday\\
");
        break;
    case 3:
        printf("Wednesday\\
");
        break;
    case 4:
        printf("Thursday\\
");
        break;
    case 5:
        printf("Friday\\
");
        break;
    case 6:
        printf("Saturday\\
");
        break;
    case 7:
        printf("Sunday\\
");
        break;
    }
    return 0;
}

If the requirements change, it becomes:

Input ?1-5, the output is “?operation?”;

Lose ?6-7 and output “rest?”

The reference code is as follows:

#include <stdio.h>
int main()
{
    int day = 0;
    scanf("%d", & amp;day);
    switch(day)
    {
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
        printf("working days\\
");
        break;
    case 6:
    case 7:
        printf("Day off\\
");
        break;
    }
    return 0;
}

At this time, someone will ask, what if you enter 8 or 9?

At this time, we will use the alternative default we just mentioned.

We add default to the above code, we can proceed like this:

#include <stdio.h>
int main()
{
    int day = 0;
    scanf("%d", & amp;day);
    switch(day)
    {
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
        printf("working day\\
");
        break;
    case 6:
    case 7:
        printf("Day off\\
");
        break;
    default:
        printf("Input error\\
");
        break;
    }
    return 0;
}

If we enter something other than 1~7, the default statement will be executed and then end.

Finally, we can see that the code above is neatly arranged from case 1 to case 7, and then to default. Someone may ask again, does this code have requirements for this format?

In fact, there is no such thing. Case 5 can also be on case 1, and default can also be on case 1. It will not affect the running process of the code. The above is written for the sake of the beauty of the code and the convenience of reading it. After all, no one wants the code they write to be messy…

Today’s content ends here. If you have any questions, you can leave a message in the comment area or send a private message.

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Algorithm skill tree Home page Overview 57211 people are learning the system