当前位置:网站首页>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 .
边栏推荐
- Mysql database basic operations DML
- OpenCV VideoCapture. Get() parameter details
- 雅思口语的具体步骤和时间安排是什么样的?
- Comparison between variable and "zero value"
- NPDP certification | how do product managers communicate across functions / teams?
- 第4章:再谈类的加载器
- [linear algebra] determinant of order 1.3 n
- Improving Multimodal Accuracy Through Modality Pre-training and Attention
- The nearest common ancestor of binary (search) tree ●●
- 空结构体多大?
猜你喜欢

数据处理技巧(7):MATLAB 读取数字字符串混杂的文本文件txt中的数据

硬件開發筆記(十): 硬件開發基本流程,制作一個USB轉RS232的模塊(九):創建CH340G/MAX232封裝庫sop-16並關聯原理圖元器件

Signed and unsigned keywords

【LeetCode】19、 删除链表的倒数第 N 个结点

Export MySQL table data in pure mode

Hardware development notes (10): basic process of hardware development, making a USB to RS232 module (9): create ch340g/max232 package library sop-16 and associate principle primitive devices

Aardio - 通过变量名将变量值整合到一串文本中

UE4蓝图学习篇(四)--流程控制ForLoop和WhileLoop

NetXpert XG2帮您解决“布线安装与维护”难题

NPDP certification | how do product managers communicate across functions / teams?
随机推荐
【雅思口语】安娜口语学习记录part1
Report on technological progress and development prospects of solid oxide fuel cells in China (2022 Edition)
手写ABA遇到的坑
Aardio - 封装库时批量处理属性与回调函数的方法
Research and investment strategy report of China's VOCs catalyst industry (2022 Edition)
【编译原理】做了一半的LR(0)分析器
在IPv6中 链路本地地址的优势
case 关键字后面的值有什么要求吗?
uniapp设置背景图效果demo(整理)
自制J-Flash烧录工具——Qt调用jlinkARM.dll方式
UDP编程
ResNet-RS:谷歌领衔调优ResNet,性能全面超越EfficientNet系列 | 2021 arxiv
重磅新闻 | Softing FG-200获得中国3C防爆认证 为客户现场测试提供安全保障
12、 Start process
Pit encountered by handwritten ABA
硬件開發筆記(十): 硬件開發基本流程,制作一個USB轉RS232的模塊(九):創建CH340G/MAX232封裝庫sop-16並關聯原理圖元器件
MySQL ---- first acquaintance with MySQL
Improving Multimodal Accuracy Through Modality Pre-training and Attention
Unity3d Learning Notes 6 - GPU instantiation (1)
云原生技术--- 容器知识点