?
Dieses Dokument verwendet PHP-Handbuch für chinesische Websites Freigeben
導致封閉 for,while 或 do-while 循環(huán)或 switch 語句終止。
當使用條件表達式和條件語句來終止循環(huán)時,使用它時很尷尬。
break ; |
---|
僅出現(xiàn)在循環(huán)體的語句(while,do,for)內(nèi)或在 switch 的語句內(nèi)。
在這個語句之后,控制權會在封閉循環(huán)或開關后立即傳輸?shù)秸Z句或聲明中,就像通過 goto 一樣。
break
.
break 語句不能用于突破多個嵌套循環(huán)。goto 語句可以用于此目的。
#include <stdio.h> int main(void){ int i = 2; switch (i) { case 1: printf("1"); case 2: printf("2"); // i==2, so execution starts at this case label case 3: printf("3"); case 4: case 5: printf("45"); break; // execution of subsequent cases is terminated case 6: printf("6"); } printf("\n"); // Compare outputs from these two nested for loops. for (int j = 0; j < 2; j++) for (int k = 0; k < 5; k++) printf("%d%d ", j,k); printf("\n"); for (int j = 0; j < 2; j++) { for (int k = 0; k < 5; k++) { // only this loop is exited by break if (k == 2) break; printf("%d%d ", j,k); } }}
輸出:
234500 01 02 03 04 10 11 12 13 14 00 01 10 11
C11 standard (ISO/IEC 9899:2011):
6.8.6.3 The break statement (p: 153)
C99 standard (ISO/IEC 9899:1999):
6.8.6.3 The break statement (p: 138)
C89/C90 standard (ISO/IEC 9899:1990):
3.6.6.3 The break statement