当前位置:网站首页>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 .
边栏推荐
猜你喜欢
The SQL response is slow. What are your troubleshooting ideas?

Management background --1 Create classification

基于 QEMUv8 搭建 OP-TEE 开发环境

自制J-Flash烧录工具——Qt调用jlinkARM.dll方式

二叉(搜索)树的最近公共祖先 ●●

Advantages of link local address in IPv6

Installation and use of labelimg

Pit encountered by handwritten ABA

rust知识思维导图xmind

HDR image reconstruction from a single exposure using deep CNN reading notes
随机推荐
Applet system update prompt, and force the applet to restart and use the new version
基於 QEMUv8 搭建 OP-TEE 開發環境
pytorch_ Yolox pruning [with code]
Export MySQL table data in pure mode
China 1,4-cyclohexanedimethanol (CHDM) industry research and investment decision-making report (2022 Edition)
如何用程序确认当前系统的存储模式?
UE4蓝图学习篇(四)--流程控制ForLoop和WhileLoop
硬件開發筆記(十): 硬件開發基本流程,制作一個USB轉RS232的模塊(九):創建CH340G/MAX232封裝庫sop-16並關聯原理圖元器件
Inno setup packaging and signing Guide
网络基础入门理解
Learn the principle of database kernel from Oracle log parsing
go多样化定时任务通用实现与封装
MySQL约束的分类、作用及用法
Clip +json parsing converts the sound in the video into text
【雅思口语】安娜口语学习记录part1
Plafond du tutoriel MySQL, bien collecté, regardez lentement
Memorabilia of domestic database in June 2022 - ink Sky Wheel
PVL EDI project case
2022-07-05 使用tpcc对stonedb进行子查询测试
sizeof关键字