Original article. Please indicate the source for reprinting. Thank you!
Author: Qinglin, blog name: feikong jingdu
The switch statement is a very common statement in C or C ++, and is often used. In addition, the switch statements we use are used together with case, default, and break statements.
The use of switch statements can be very simple or complex, so you can see some of the following alternative uses.
First, do you know how the switch syntax is defined?
Anyone who has learned how to compile knows that if we want to write a language, the compiler must define a grammar of the language so that we can write the compiler according to the defined syntax, the syntax of C and C ++ switch statements is defined as follows:
Switch (expression) Statement
Take a look at the syntax defined above. The switch statement is followed by a left brace followed by the switch keyword, followed by an expression, followed by a right brace, followed by a statement block.
Therefore, this switch statement is very simple, so we can write the following code according to this syntax:
# Include <stdio. h> <br/> int main (INT argc, const char * argv []) <br/>{< br/> int I = 3; <br/> switch (I) <br/> I = 2; <br/> printf ("I = % d/N", I ); <br/> return 0; <br/>}
Compilation output: I = 3
We can see that the above code is very easy to use when using the switch:
Switch (expression)
Statement
We can see that the I in the switch expression does not match, so the output I value is 3.
Since the switch is followed by a statement block, our expression can be written as follows:
Switch (I)
{
I = 2;
}
In this way, use {} to box a statement block.
We want to add the default statement:
Switch (I)
{
Default:
I = 2;
}
The syntax of the switch statement is simple, but the definition of the statement block is not that simple. We can use this block flexibly to use the switch statement.
Let's take a look at the switch and case usage:
Switch (I)
{
Default:
I = 2;
Case 1:
Printf ("A = 1/N ");
Break;
}
A case must be followed by an integer or an object that can be converted to an integer. However, case does not have to be the first layer in the statement block, so we can write it as follows:
# Include <stdio. h> <br/> int main (INT argc, const char * argv []) <br/>{< br/> int I = 3; <br/> switch (I) <br/>{< br/> if (I <3) <br/>{< br/> case 1: <br/> I + = 1; <br/> break; <br/> case 2: <br/> I + = 2; <br/> break; <br/>}< br/> else <br/>{< br/> case 3: <br/> I + = 5; <br/> break; <br/> default: <br/> I + = 10; <br/>}</P> <p> printf ("I = % d/N", I); <br/> return 0; <br/>}
Compilation output: I = 8;
As case implementation is actually a jump statement from the Assembly perspective, you can use the following code style:
# Include <stdio. h> <br/> # define new 1 <br/> # define del 2 </P> <p> int main (INT argc, const char * argv []) <br/>{< br/> int * P = new int; <br/> * P = 2; <br/> int I = new; <br/> int * Pi = NULL; <br/> switch (I) <br/>{< br/> case new: <br/> Pi = new int; <br/> If (PI) <br/> {<br/> * Pi = 3; <br/> break; <br/>}< br/> else <br/>{< br/> case DEL: <br/> Delete P; <br/> Delete PI; <br/>}< br/> default: <br/> break; <br/>}< br/> printf ("P = % d, pi = % d/N ", * P, * PI); <br/> return 0; <br/>}
Compilation output: P = 2, Pi = 3
Although the above Code has no practical significance, we can also see the usage of alternative switch statements. The key to the usage of switch statements is the subsequent statement block, this statement block can be used flexibly to flexibly use switch statements.