咱們可在程序中使用break關鍵字用於退出當前循環。code
int x; for(int i = 0 ; i < 10 ; i++){ if(i == 1){ //當i=1時,跳到下一次循環 break; } x++; }
通常咱們會用while
或者if
來進行條件判斷:get
while(arr[i] > x){ return i; } if(arr[i] == target){ return i; }
但因爲while
也是一個循環體,當在whlie
中使用break只是跳出當前的while
循環:循環
while(i){ while( i==1){ break; //只會跳出當前的while,i--仍會執行 } i--; }