当前位置:网站首页>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 .
边栏推荐
- 中国固态氧化物燃料电池技术进展与发展前景报告(2022版)
- Heavyweight news | softing fg-200 has obtained China 3C explosion-proof certification to provide safety assurance for customers' on-site testing
- Aardio - 封装库时批量处理属性与回调函数的方法
- volatile关键字
- (18) LCD1602 experiment
- LeetCode 练习——剑指 Offer 26. 树的子结构
- Export MySQL table data in pure mode
- SQL server generates auto increment sequence number
- 2021 geometry deep learning master Michael Bronstein long article analysis
- China 1,4-cyclohexanedimethanol (CHDM) industry research and investment decision-making report (2022 Edition)
猜你喜欢
Mise en place d'un environnement de développement OP - tee basé sur qemuv8
自定义 swap 函数
signed、unsigned关键字
云原生技术--- 容器知识点
MySQL----初识MySQL
[Digital IC hand tearing code] Verilog burr free clock switching circuit | topic | principle | design | simulation
二叉(搜索)树的最近公共祖先 ●●
The nearest common ancestor of binary (search) tree ●●
Sword finger offer question brushing record 1
MySQL ---- first acquaintance with MySQL
随机推荐
signed、unsigned关键字
BasicVSR_PlusPlus-master测试视频、图片
云原生技术--- 容器知识点
Management background --4, delete classification
Unity3d minigame-unity-webgl-transform插件转换微信小游戏报错To use dlopen, you need to use Emscripten‘s...问题
每日一题:力扣:225:用队列实现栈
Aardio - 通过变量名将变量值整合到一串文本中
MySQL教程的天花板,收藏好,慢慢看
const关键字
自定义 swap 函数
2021 geometry deep learning master Michael Bronstein long article analysis
剪映+json解析将视频中的声音转换成文本
变量与“零值”的比较
void关键字
基于 QEMUv8 搭建 OP-TEE 开发环境
rust知识思维导图xmind
Adavit -- dynamic network with adaptive selection of computing structure
extern关键字
Unity3d Learning Notes 6 - GPU instantiation (1)
[linear algebra] determinant of order 1.3 n