- 4.8.4 switch语句
- 4.8.4.1 缩进
- 4.8.4.2 Fall-through:注释
- 4.8.4.3 default的情况要写出来
4.8.4 switch语句
术语说明:switch块的大括号内是一个或多个语句组。每个语句组包含一个或多个switch标签(case FOO:或default:),后面跟着一条或多条语句。
4.8.4.1 缩进
与其它块状结构一致,switch块中的内容缩进为2个空格。
每个switch标签后新起一行,再缩进2个空格,写下一条或多条语句。
4.8.4.2 Fall-through:注释
在一个switch块内,每个语句组要么通过break, continue, return或抛出异常来终止,要么通过一条注释来说明程序将继续执行到下一个语句组, 任何能表达这个意思的注释都是OK的(典型的是用// fall through)。这个特殊的注释并不需要在最后一个语句组(一般是default)中出现。示例:
switch (input) {case 1:case 2:prepareOneOrTwo();// fall throughcase 3:handleOneTwoOrThree();break;default:handleLargeNumber(input);}
4.8.4.3 default的情况要写出来
每个switch语句都包含一个default语句组,即使它什么代码也不包含。
