当前位置:网站首页>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 .
边栏推荐
猜你喜欢
Unity3d minigame-unity-webgl-transform插件转换微信小游戏报错To use dlopen, you need to use Emscripten‘s...问题
Config:invalid signature solution and troubleshooting details
RESNET rs: Google takes the lead in tuning RESNET, and its performance comprehensively surpasses efficientnet series | 2021 arXiv
Web APIs DOM 时间对象
(18) LCD1602 experiment
硬件開發筆記(十): 硬件開發基本流程,制作一個USB轉RS232的模塊(九):創建CH340G/MAX232封裝庫sop-16並關聯原理圖元器件
rust知识思维导图xmind
[leetcode] 19. Delete the penultimate node of the linked list
CocosCreator+TypeScripts自己写一个对象池
UE4蓝图学习篇(四)--流程控制ForLoop和WhileLoop
随机推荐
Mysql database basic operations DML
将MySQL的表数据纯净方式导出
Unity3d Learning Notes 6 - GPU instantiation (1)
i. Mx6ull build boa server details and some of the problems encountered
Management background --4, delete classification
软考高级(信息系统项目管理师)高频考点:项目质量管理
网络基础入门理解
关于声子和热输运计算中BORN电荷和non-analytic修正的问题
Seata聚合 AT、TCC、SAGA 、 XA事务模式打造一站式的分布式事务解决方案
机试刷题1
新手程序员该不该背代码?
在IPv6中 链路本地地址的优势
Attack and defense world miscall
extern关键字
Mise en place d'un environnement de développement OP - tee basé sur qemuv8
Management background --2 Classification list
[linear algebra] determinant of order 1.3 n
What are the interface tests? What are the general test points?
【踩坑合辑】Attempting to deserialize object on CUDA device+buff/cache占用过高+pad_sequence
Aardio - 利用customPlus库+plus构造一个多按钮组件