用户工具

站点工具


en:reference:language:switchcase
no way to compare when less than two revisions

差别

这里会显示出您选择的修订版和当前版本之间的差别。


前一修订版
en:reference:language:switchcase [2023/06/07 04:23] (当前版本) – 外部编辑 127.0.0.1
行 1: 行 1:
 +====== switch / case statements ======
 +
 +Like [[zh:reference:language:if|if]] statements, switch...case controls the flow of programs by allowing programmers to specify different code that should be executed in various conditions. In particular, a switch statement compares the value of a variable to the values specified in case statements. When a case statement is found whose value matches that of the variable, the code in that case statement is run.
 +
 +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 ("falling-through") until a break, or the end of the switch statement is reached.
 +
 +===== 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
 +  }
 +
 +</code> 
 +===== Syntax =====
 +<code cpp>
 +switch (var) {
 +  case label:
 +    // statements
 +    break;
 +  case label:
 +    // statements
 +    break;
 +  default: 
 +    // statements
 +}
 +
 +</code> 
 +===== Parameters =====
 +
 +var: the variable whose value to compare to the various cases
 +
 +label: a value to compare the variable to
  
en/reference/language/switchcase.txt · 最后更改: 2023/06/07 04:23 由 127.0.0.1