int x=10; do { System.out.println("value of x:"+x); x++; } while(x<20); //do while循環
1 int x=10; 2 while(x<20) { 3 System.out.println("value of x:"+x); 4 x++; 5 } //while循環
while(布爾表達式) 只要布爾表達式內容為真 循環體就會一直循環下去
do while 即使不滿足條件 也至少執行一次
1 for(int x=10;x<20;x=x+1) 2 { 3 System.out.println("value of x:" +x); 4 } //for循環 for語句 和while 語句后面沒有;
循環次數在循環執行前就已經確定了
注意for循環內部用 ;
1 int [] numbers= {10,20,30,40,50,60}; 2 for (int x:numbers) { 3 System.out.print(x); 4 System.out.print(','); 5 } 6 System.out.print("\n"); 7 String []names = {"Jame","Tom","Jerry"}; 8 for(String name : names) { 9 System.out.print(name); 10 System.out.print(','); 11 } //數組增強for循環
Java增強for循環
for(聲明語句:表達式)
聲明語句:聲明新的局部變量,變量類型必須與數組元素匹配
表達式:要訪問的數組名,或者是返回值為數組的方法
1 int [] numbers= {10,20,30,40,50}; 2 for(int x:numbers) { 3 if(x==30) { 4 break; 5 } 6 System.out.println(x); 7 } //break關鍵字
break通常應用于switch語句中,用于跳出循環
break跳出最里層的循環 并且執行該循環下面的語句
1 int [] numbers= {10,20,30,40,50}; 2 for(int x:numbers) { 3 if(x==30) { 4 continue; 5 } 6 System.out.println(x); 7 } //continue關鍵字
continue適用于許多的循環結構
for循環中,continue語句使程序立即跳轉到更新語句
浙公網安備 33010602011771號