当前位置:网站首页>In depth C language (4) -- definition and use of switch
In depth C language (4) -- definition and use of switch
2022-07-29 03:33:00 【It is said that this is ZZY】
thorough C Language (4)——switch The definition and use of
Used in the full text C The standard for the C99,ANSI C, If there is no special description, the default is .
C++ It is Dev 5.7.1 Of C++ edition .
C Of Switch and case
Common grammar :
switch (expression)
statement
among expression The result of must be an integer value .
Although it is possible to use a statement directly , But the results are as follows :
switch (expression)
{
statement-list
}
Throughout statement-list The label of is as follows :
case constant-expression:
every last case The tag must have a unique value :
Constant expression (constant-expression),constant-expression It refers to the expression evaluated during compilation , It cannot be any variable .
case With the execution flow
case The most special point of the statement is , It does not divide the statement into several execution parts , But through case To determine the entry point of the statement list .
The execution process is as follows :
- The calculation is complete expression The content of .
- The execution flows to its... In the statement list case The tag value is the same as expression Statements with matching values .
- Start from the execution of the previous statement , Until switch-case The bottom of .
All the statements will be executed .
for instance :
switch (month)
{
case 12:
printf("%d",12);
case 11:
printf("%d",11);
}
The result? , It may be different from what you think .
When month be equal to 12 When , give the result as follows :
1211
This is the result of execution flow , Lead to case The statements behind the tag will be executed , Including others case.
Can you realize the function of dividing the code into different parts ? Of course. ,break The statement is .
break sentence
break The statement is very simple , Is to make case The flow stops at different positions . Give Way switch Run in a more traditional way .
How to use it is as follows :
switch (month)
{
case 12:
printf("%d",12);
break;
case 11:
printf("%d",11);
case 10:
printf("%d",10);
}
give the result as follows :
12
12
--------------------------------
Process exited after 4.971 seconds with return value 0
Please press any key to continue . . .
break The habit of
One caveat , final case The need for break Do you ? Theoretically speaking, traditional break In the syntax , We don't need to be in the last case Add break. Because no case Will continue to execute , But for the consistency of future modifications , Let's add at the end break, Add it later case There is no need to add options .
In order to allow different expression values to be executed , Can make multiple case The label corresponding , As shown below :
switch (month)
{
case 12:
printf("%d",12);
break;
case 11:
case 10:
printf("%d %d",10,11);
break;
}
give the result as follows :
11
1011
--------------------------------
10
1011
--------------------------------
default Clause
If there is a situation , all case The tag values of do not match , But you want to find a condition to cover the bottom , You can use default Clause , Put the label below case Where you can be .
default:
That is to say, you can write like this :
switch (month)
{
case 12:
printf("%d",12);
break;
default :
printf("%s","default");
case 11:
case 10:
printf("%d %d",10,11);
break;
}
give the result as follows :
9
default1011
--------------------------------
So it's lost default The meaning of , But it's still interesting hhhh.
边栏推荐
- Practical guidance for interface automation testing (Part I): what preparations should be made for interface automation
- makefile详解
- ShardingSphere之水平分表实战(三)
- Regular expression bypasses WAF
- NXP i.mx8mp-deepviewrt
- Self study notes on Apache file management -- mapping folders and configuring Apache virtual machines based on single IP and multi domain names
- Ten thousand words detailed Google play online application standard package format AAB
- 实例搭建Flask服务(简易版)
- Photo scale correction tool: DxO viewpoint 3 direct mount version
- Simple code implementation of K-means clustering
猜你喜欢
(2022杭电多校三)1011-Link is as bear(思维+线性基)
Singleton mode (hungry and lazy)
What is eplato cast by Plato farm on elephant swap? Why is there a high premium?
Ten thousand words detailed Google play online application standard package format AAB
Matlab learning -- structured programs and user-defined functions
The Federal Reserve raised interest rates again, Powell "let go of doves" at 75 basis points, and US stocks reveled
Bingbing learning notes: operator overloading -- implementation of date class
Practical application cases of digital Twins - smart energy
ROS - create workspace
(2022杭电多校三)1002-Boss Rush(状压DP+二分)
随机推荐
Simple code implementation of K-means clustering
Complexity analysis learning
Mathematical modeling -- analytic hierarchy process model
Machine learning [numpy]
Web uploader cannot upload multiple files
How to solve the time zone problem in MySQL timestamp
实例搭建Flask服务(简易版)
(nowcoder22529C)dinner(容斥原理+排列组合)
Matlab learning -- structured programs and user-defined functions
(newcoder 15079)无关(容斥原理)
i. MX 8m plus integrated dedicated neural processing engine (NPU)
容斥原理
腾讯云使用pem登录
深入C语言(4)——switch的定义与使用
Introduction and advanced level of MySQL (11)
Calculation of array serial number of force deduction questions (daily question 7/28)
C obtains JSON format data asynchronously from the web address
1.5 nn. Module neural network (III)
The difference between int and integer. Is int or integer used in practical applications?
Rongyun IM & RTC capabilities on new sites