当前位置:网站首页>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.
边栏推荐
- Suffix automata (SAM) board from Jly
- Mathematical modeling -- analytic hierarchy process model
- Microcomputer principle operation
- RTP 发送 和接收 h265
- three. JS Part 54 how to pass structure array to shader
- Notes on letter symbol marking of papers
- 复现20字符短域名绕过以及xss相关知识点
- 【C】 Array
- July 28, 2022 Gu Yujia's study notes
- Idea configuration web container and war packaging
猜你喜欢

Multi level wavelet CNN for image restoration

Configure vscade to realize ROS writing

(2022杭电多校三)1011-Link is as bear(思维+线性基)

Pp-yoloe details

Bingbing learning notes: operator overloading -- implementation of date class
![Leetcode 1331 array sequence number conversion [map] the leetcode path of heroding](/img/be/d429d0c437dc5ed7cb4448e223a83a.png)
Leetcode 1331 array sequence number conversion [map] the leetcode path of heroding

Violence recursion to dynamic programming 01 (robot movement)

深入C语言(1)——操作符与表达式

What is eplato cast by Plato farm on elephant swap? Why is there a high premium?

How to solve the time zone problem in MySQL timestamp
随机推荐
How to realize multi line annotation in MATLAB
容斥原理
Bingbing learning notes: operator overloading -- implementation of date class
CUDA GDB prompt: /tmp/tmpxft**** cudafe1.stub. c: No such file or directory.
Complexity analysis learning
Flask creation process day05-06 creation project
正则表达绕过waf
Three military product baselines (functional baseline, distribution baseline, product baseline) and the documents contained in the baseline
Several methods of converting object to string
Introduction and advanced level of MySQL (11)
Minesweeping simple version
web-uploader不能多文件上传
Mathematical modeling -- analytic hierarchy process model
Introduction to static routing and dynamic routing protocols OSPF and rip and static routing configuration commands
Arm architecture and neural network
Idea configuration web container and war packaging
Regular expression bypasses WAF
Photo scale correction tool: DxO viewpoint 3 direct mount version
Machine learning [numpy]
Machine learning based on deepchem