当前位置:网站首页>C traps and defects Chapter 2 lexical "traps" 2.4 switch statements
C traps and defects Chapter 2 lexical "traps" 2.4 switch statements
2022-07-25 01:30:00 【weixin_ Guest time】
switch sentence
C Linguistic switch The control flow of statements can pass through and execute each case part , This is C The difference of language .
The two programs are used separately C Language and Pascal Statement writing :
switch(color) {
case 1: printf("red");
break;
case 2: printf("yellow");
break;
case 3: printf("blue");
break;
}
switch(color) {
case 1: printf("red");
case 2: printf("yellow");
case 3: printf("blue");
}
The two pieces of code need to complete the same task . According to the variable color Value (1、2 or 3), Print out separately red、yellow or blue. There is only one exception : That's using Pascal Each of the program segments written in case Some do not C Linguistic break The corresponding part of the statement . The reason for this , Because C In language case Label as a real label , So the control flow of the program will go straight through case label , Without any impact . And in the Pascal in , Every case The label will implicitly end the previous case part .
use C Write the following program in
switch(color) {
case 1: printf("red");
case 2: printf("yellow");
case 3: printf("blue");
}
Further assume that variables color The value of is 2. Last , The program will print out
yellowblue
Because the control flow of the program is executing the second printf After the call of the function , It will naturally be executed in sequence , So the third one printf Function calls are also executed .
C In language switch This feature of the statement , Is its advantage , It is also one of its weaknesses . Say it's a weakness , It's because it's easy for programmers to miss everything case Part of the break sentence , Cause some difficult problems . Say it's the advantage , Because if the program wants to intentionally omit one break sentence , It can express some program control structures that are difficult to be easily realized in other ways . Especially for some big switch sentence , We often find that the processing of each branch is similar : Make a slight change to a branch , The rest is exactly the same as in the case of another branch .
The program has a switch sentence , Used to handle each different opcode . On this hypothetical computer , Just reverse the sign of the second operand , Subtraction and addition are essentially the same .
case SUBTRACT:
opnd2 = -opnd2;
/* There is no break sentence */
case ADD:
...
Of course , Adding appropriate comments like the above example is a good practice . People who read this code can know that one is intentionally omitted here break sentence .
Its function is to skip the white space characters in the program when the compiler looks for symbols . here , Space bar 、 The processing of tab and newline characters is the same , However, when encountering line breaks , The code line count of the program should be incremented :
case '\n':
linecount++;
/* There is no break sentence */
case '\t':
case ' ':
...
边栏推荐
- Open source demo | release of open source example of arcall applet
- DotNetCore. Cap notes
- Three modes of executing programs, memory and cache
- The position of the nth occurrence of MySQL in the string
- Start to build a three node Eureka cluster
- Green low-carbon Tianyi cloud, a new engine of digital economy!
- SAP Spartacus - progressive web applications, progressive web applications
- Top priority of dry goods: common indicators and terms in data analysis!
- ES6 modularization
- A string "0" was actually the culprit of the collapse of station b
猜你喜欢

Musk responded whether he would upload his brain to the cloud: already did it!

【Power Shell】Invoke-Expression ,Invoke-Expression -Command $activateCommand; Error or power shell failed to activate the virtual environment

Implementing DDD based on ABP -- domain logic and application logic

Green low-carbon Tianyi cloud, a new engine of digital economy!
![[programmer interview classic] 01.09 string rotation](/img/d2/7ea9351c31af01665d86f8c6bc3468.png)
[programmer interview classic] 01.09 string rotation

The first meta universe auction of Chen Danqing's printmaking works will open tomorrow!

C#/VB. Net insert watermark in word

Pursue and kill "wallet Assassin" all over the network

Harbor installation

Password input box and coupon and custom soft keyboard
随机推荐
Research 280+ documents! The team of Tsinghua Huang Gao, the winner of CVPR best paper, proposed the first dynamic network overview to comprehensively review the development of dynamic networks!
[programmer interview classic] 01.09 string rotation
Data governance notes
Management mode of agricultural science data center based on life cycle theory
Kubernetes creates a user with dashboard read-only permission (with exec permission)
Performance analysis method - Notes on top of performance
Redis learning notes (2) - power node of station B
EasyX realizes button effect
A string "0" was actually the culprit of the collapse of station b
7.14 - daily question - 408
2022/7/18-7/19
Unity slider slider development
[summer daily question] Luogu p1605 maze
Password input box and coupon and custom soft keyboard
1260. Two dimensional grid migration: simple construction simulation problem
Nacos hand to hand teaching [i] dynamic configuration of Nacos
DotNetCore. Cap notes
MySQL series | log module
Pursue and kill "wallet Assassin" all over the network
Web Security Foundation - file upload