这里会显示出您选择的修订版和当前版本之间的差别。
前一修订版 | |||
— | en:reference:language:switchcase [2023/06/07 04:23] (当前版本) – 外部编辑 127.0.0.1 | ||
---|---|---|---|
行 1: | 行 1: | ||
+ | ====== switch / case statements ====== | ||
+ | |||
+ | Like [[zh: | ||
+ | |||
+ | The break keyword exits the switch statement, and is typically used at the end of each case. Without a break statement, the switch statement will continue executing the following expressions (" | ||
+ | |||
+ | ===== Example ===== | ||
+ | <code cpp> | ||
+ | switch (var) { | ||
+ | case 1: | ||
+ | //do something when var equals 1 | ||
+ | break; | ||
+ | case 2: | ||
+ | //do something when var equals 2 | ||
+ | break; | ||
+ | default: | ||
+ | // if nothing else matches, do the default | ||
+ | // default is optional | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | ===== Syntax ===== | ||
+ | <code cpp> | ||
+ | switch (var) { | ||
+ | case label: | ||
+ | // statements | ||
+ | break; | ||
+ | case label: | ||
+ | // statements | ||
+ | break; | ||
+ | default: | ||
+ | // statements | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | ===== Parameters ===== | ||
+ | |||
+ | var: the variable whose value to compare to the various cases | ||
+ | |||
+ | label: a value to compare the variable to | ||