break 语句 (C)break Statement (C)

break 语句将终止执行其所在位置最接近的外围 doforswitchwhile 语句。The break statement terminates the execution of the nearest enclosing do, for, switch, or while statement in which it appears. 控制权将传递给已终止语句后面的语句。Control passes to the statement that follows the terminated statement.

语法Syntax

jump-statement:jump-statement:
break;

break 语句通常用于终止 switch 语句中的特定用例的处理。The break statement is frequently used to terminate the processing of a particular case within a switch statement. 缺少外围的迭代或 switch 语句会引发错误。Lack of an enclosing iterative or switch statement generates an error.

在嵌套语句中,break 语句只终止直接包围它的 doforswitchwhile 语句。Within nested statements, the break statement terminates only the do, for, switch, or while statement that immediately encloses it. 可以在嵌套结构外部的其他地方使用 returngoto 语句来移交控制权。You can use a return or goto statement to transfer control elsewhere out of the nested structure.

以下示例演示了 break 语句:This example illustrates the break statement:

#include <stdio.h>  
int main() {  
   char c;  
   for(;;) {  
      printf_s( "\nPress any key, Q to quit: " );  

      // Convert to character value  
      scanf_s("%c", &c);  
      if (c == 'Q')  
          break;  
   }  
} // Loop exits only when 'Q' is pressed  

另请参阅See Also

break 语句break Statement