Home  >  Article  >  Backend Development  >  What must be the value after case in the switch statement?

What must be the value after case in the switch statement?

烟雨青岚
烟雨青岚Original
2020-07-07 14:31:0619785browse

The value after case in the switch statement must be a constant. Because in the switch statement, a jump table will be generated based on the constant value behind the case label. After only a few comparisons, you can jump to the corresponding label; if variables are allowed, switch can only be performed from beginning to end on a piece of data. Comparison loses its meaning.

What must be the value after case in the switch statement?

#The value after case in the switch statement must be a constant.

Because switch in C language is not a replacement for if.

The switch will be optimized during compilation, and a jump table will be generated based on the constant value behind the case label. After only a few times of comparison, you can jump to the corresponding label.

So, labels cannot be repeated. If variables are allowed, switch can only degenerate into a series of if else, and a piece of data can only be compared from beginning to end, thus losing the meaning of switch.

The complexity of jump table and comparison one by one is much different between these two methods.

This is also one of the reasons why python does not introduce a control structure corresponding to the switch case in C language.

General expression:

switch (表达式)
{
    case 常量表达式1:    语句1
    case 常量表达式2:    语句2
       ┇
    case 常量表达式n:    语句n
    default:        语句n+1
}

switch means "switch", it is also a "select" statement, but its usage is very simple. switch is a multi-branch selection statement. To put it simply, multiple branches are multiple ifs.

Functionally speaking, switch statements and if statements can completely replace each other. But from a programming perspective, they each have their own characteristics, so so far it cannot be said that one can completely replace the other.

When there are relatively few nested ifs (within three), it will be simpler to write a program using if.

But when there are many branches selected, there will be many levels of nested if statements, resulting in a lengthy program and reduced readability.

So C language provides switch statement to handle multiple branch selections. So if and switch can be said to have a clear division of labor. In many large projects, multi-branch selection situations are often encountered, so switch statements are still used quite often.

Recommended tutorial: "C Language"

The above is the detailed content of What must be the value after case in the switch statement?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn