当前位置:网站首页>Is there any requirement for the value after the case keyword?
Is there any requirement for the value after the case keyword?
2022-07-06 22:30:00 【It's Beichen not too PI acridine】
case keyword
switch(variable)
{
case Value1:
//program code
break;
case Value2:
//program code
break;
case Value3:
//program code
break;
…
default:
break;
}
1. Every case Never forget to add... At the end of the sentence break, Otherwise, multiple branches will overlap ( Unless you intentionally overlap multiple branches ).
2. Must be used finally default Branch . Even if the program really doesn't need default Handle , You should also keep the statement :
default:
break;
This is not to paint the snake , It can avoid making people think you have forgotten default Handle .
case Is there any requirement for the value after the keyword ?
case Can only be followed by integer or character constants or constant expressions ( Think about how character data is stored in memory ).
A character constant is a character enclosed in a pair of single quotation marks . Such as ‘a’,‘9’,‘!’. Generally, it is used as integer data for operation .
Constant expression refers to the expression whose value will not change and the calculation result can be obtained during compilation
A constant expression is an expression in which there are only constants , such as 1+2 It's a constant expression , If you define a For constant 1, that a+2 It's also a constant expression . If you define variables a, that a+2 It's not a constant expression .n=5 It's a constant expression .
How is character data stored in memory ?
Character type data is stored in memory ASCII Code value , It is a byte , All data types are in memory with 0 and 1 Code stored in binary , This principle will not change .
stay C In language ,char Type data is to put a character constant into a character variable , Instead of putting the character itself in a memory unit , Instead, the corresponding ASCII Put the code in the storage unit .
In the encoding , A Chinese character storage needs 2 Bytes . stay UTF-8 In the encoding , An alphabetic character store needs 1 Bytes , A Chinese character storage needs 3 To 4 Bytes . stay UTF-16 In the encoding , An English alphabetic character or a Chinese character needs to be stored 2 Bytes . stay UTF-32 In the encoding , Any character in the world needs to be stored 4 Bytes .
case The order of statements
If case Few statements , You may be able to ignore this , But if case There are so many sentences , Then I have to consider this problem carefully . For example, you write a driver , You may often encounter dozens case Statement . Generally speaking , We can follow the following rules :
1. Arrange the items in alphabetical or numerical order case sentence .
If all case There is no obvious difference in the importance of sentences , Then press A-B-C or 1-2-3 In equal order case sentence . In doing so , You can easily find one case sentence .
switch(variable)
{
case A:
//program code
break;
case B:
//program code
break;
case C:
//program code
break;
…
default:
break;
}
2. Put the normal situation first , And put the abnormal situation behind .
If there are multiple normal and abnormal conditions , Put the normal situation first , And make notes ; Put the abnormal situation behind , Also make notes .
switch(variable)
{
///
// Under normal circumstances
case A:
//program code
break;
case B:
//program code
break;
// The normal situation ends
//
// Abnormal conditions begin
case -1:
//program code
break;
// The abnormal situation ends
//
…
default:
break;
}
3. Arrange by execution frequency case sentence .
Put the most frequently implemented cases first , And put the least frequently implemented cases behind . The most frequently executed code may also be the code that needs to be executed in one step during debugging . If you put it in the back , It may be difficult to find , And put it in the front , You can find it quickly .
Use case Other considerations for statements
1. Simplify the operation for each case .
Make the code related to each situation as concise as possible .case The more refined the code after the statement ,case The clearer the result of the statement . Do you think , If case The code behind the statement can't fit on the whole screen , No one can see such code clearly . If a case Statements do require so much code to perform an operation , Then these operations can be written into one or several subroutines , And then in case Call these subroutines after the statement ok 了 . Generally speaking case The code behind the statement should not exceed 20 That's ok .
2. Don't try to use case Statement and deliberately create a variable .
In order to use case And deliberately constructed variables are easy to confuse people , Such variables should be avoided .
char action = a[0];
switch (action)
{
case ‘c’:
fun1();
break;
case ‘d’:
break;
…
default:
break;
}
Control here case The variable of the statement is action. and action The value of is an array of characters a A character of . But this way may bring some hidden mistakes . generally speaking , When you want to use case Statement and deliberately create a variable , Real data may not map to... In the way you want case In the sentence . In this case , If the user enters a character array a What's in it is “const” This string , that case The statement will match to the first case On , And call fun1() function . However, if there are other characters in this array c Any string at the beginning ( such as :“col”,“can”),case Branches will also match to the first case On . But this may not be the result you want , This implied mistake often drives people crazy .
3. hold default Clause is only used to check for true defaults .
occasionally , You only have one last situation to deal with , So I decided to use this situation default Clause to handle . This may make you lazy and less typing a few characters , But this is very unwise . This will lose case The self explanatory function provided by the label of the statement , And also lost its use default Clause's ability to handle error situations . therefore , I advise you not to be lazy , Use every situation honestly case Statement to complete , And leave the real default situation to default Clause .
边栏推荐
- 【编译原理】做了一半的LR(0)分析器
- 关于声子和热输运计算中BORN电荷和non-analytic修正的问题
- Installation and use of labelimg
- (十八)LCD1602实验
- 3DMAX assign face map
- [Digital IC hand tearing code] Verilog burr free clock switching circuit | topic | principle | design | simulation
- Signed and unsigned keywords
- Plafond du tutoriel MySQL, bien collecté, regardez lentement
- How do I write Flask's excellent debug log message to a file in production?
- 12、 Start process
猜你喜欢
leetcode:面试题 17.24. 子矩阵最大累加和(待研究)
Management background --4, delete classification
Assembly and interface technology experiment 5-8259 interrupt experiment
pytorch_ Yolox pruning [with code]
Aardio - 通过变量名将变量值整合到一串文本中
【编译原理】做了一半的LR(0)分析器
Sword finger offer question brushing record 1
Attack and defense world ditf Misc
Senior soft test (Information System Project Manager) high frequency test site: project quality management
新手程序员该不该背代码?
随机推荐
Windows Auzre 微软的云计算产品的后台操作界面
Anaconda installs third-party packages
pytorch_ Yolox pruning [with code]
Adavit -- dynamic network with adaptive selection of computing structure
UDP programming
MySQL约束的分类、作用及用法
CCNA Cisco network EIGRP protocol
CocosCreator+TypeScripts自己写一个对象池
Inno Setup 打包及签名指南
Unity3d minigame-unity-webgl-transform插件转换微信小游戏报错To use dlopen, you need to use Emscripten‘s...问题
MySQL教程的天花板,收藏好,慢慢看
signed、unsigned关键字
Config:invalid signature solution and troubleshooting details
NPDP认证|产品经理如何跨职能/跨团队沟通?
That's why you can't understand recursion
LeetCode 练习——剑指 Offer 26. 树的子结构
ThreadLocal详解
rust知识思维导图xmind
中国VOCs催化剂行业研究与投资战略报告(2022版)
[linear algebra] determinant of order 1.3 n