Java 循環結構
順序結構的程式語句只能執行一次。如果您想要同樣的操作執行多次,,就需要使用循環結構。
Java中有三種主要的循環結構:
while迴圈
do…while循環
for迴圈
在Java5中引進了一個主要用於陣列的增強型for迴圈。
while循環
while是最基本的循環,它的結構為:
while( 布尔表达式 ) { //循环内容 }
只要布林表達式為true,循環體就會一直執行下去。
實例
public class Test { public static void main(String args[]) { int x = 10; while( x < 20 ) { System.out.print("value of x : " + x ); x++; System.out.print("\n"); } } }
以上實例編譯運行結果如下:
value of x : 10 value of x : 11 value of x : 12 value of x : 13 value of x : 14 value of x : 15 value of x : 16 value of x : 17 value of x : 18 value of x : 19
do…while循環
對於while語句而言,如果不滿足條件,則不能進入循環。但有時候我們需要即使不符合條件,也至少執行一次。
do…while迴圈和while迴圈相似,不同的是,do…while迴圈至少會執行一次。
do { //代码语句 }while(布尔表达式);
注意:布林表達式在循環體的後面,所以語句區塊在偵測布林運算式之前已經執行了。 如果布林運算式的值為true,則語句區塊一直執行,直到布林運算式的值為false。
實例
public class Test { public static void main(String args[]){ int x = 10; do{ System.out.print("value of x : " + x ); x++; System.out.print("\n"); }while( x < 20 ); } }
以上實例編譯運行結果如下:
value of x : 10 value of x : 11 value of x : 12 value of x : 13 value of x : 14 value of x : 15 value of x : 16 value of x : 17 value of x : 18 value of x : 19
for循環
雖然所有循環結構都可以用while或do. ..while表示,但Java提供了另一種語句- for循環,使一些循環結構變得更加簡單。
for迴圈執行的次數是在執行前就確定的。文法格式如下:
for(初始化; 布尔表达式; 更新) { //代码语句 }
關於for迴圈有以下幾點說明:
最先執行初始化步驟。可以宣告一種類型,但可初始化一個或多個循環控制變量,也可以是空語句。
然後,檢測布林表達式的值。如果為true,則循環體被執行。如果為false,迴圈終止,開始執行迴圈體後面的語句。
執行一次迴圈後,更新循環控制變數。
再次檢測布林表達式。循環執行上面的過程。
實例
public class Test { public static void main(String args[]) { for(int x = 10; x < 20; x = x+1) { System.out.print("value of x : " + x ); System.out.print("\n"); } } }
以上實例編譯運行結果如下:
value of x : 10 value of x : 11 value of x : 12 value of x : 13 value of x : 14 value of x : 15 value of x : 16 value of x : 17 value of x : 18 value of x : 19
Java增強for循環
Java5引入了一種主要用於數組的增強型for迴圈。
Java增強for迴圈語法格式如下:
for(声明语句 : 表达式) { //代码句子 }
宣告語句:宣告新的局部變量,該變數的型別必須與陣列元素的型別相符。其作用域限定在迴圈語句塊,其值與此時數組元素的值相等。
表達式:表達式是要存取的陣列名,或是傳回值為陣列的方法。
實例
public class Test { public static void main(String args[]){ int [] numbers = {10, 20, 30, 40, 50}; for(int x : numbers ){ System.out.print( x ); System.out.print(","); } System.out.print("\n"); String [] names ={"James", "Larry", "Tom", "Lacy"}; for( String name : names ) { System.out.print( name ); System.out.print(","); } } }
以上實例編譯運行結果如下:
10,20,30,40,50, James,Larry,Tom,Lacy,
break關鍵字
break主要用在循環語句或switch語句中,用來跳出整個語句塊。
break跳出最裡層的循環,並且繼續執行該循環下面的語句。
語法
break的用法很簡單,就是循環結構中的一個語句:
break;
實例
public class Test { public static void main(String args[]) { int [] numbers = {10, 20, 30, 40, 50}; for(int x : numbers ) { if( x == 30 ) { break; } System.out.print( x ); System.out.print("\n"); } } }
以上實例編譯運行結果如下:
10 20
continue關鍵字
continue適用於任何循環控制結構中。作用是讓程式立刻跳到下一次循環的迭代。
在for迴圈中,continue語句使程式立即跳到更新語句。
在while或do…while迴圈中,程式立即跳到布林運算式的判斷語句。
語法
continue就是循環體中一條簡單的語句:
continue;
實例
public class Test { public static void main(String args[]) { int [] numbers = {10, 20, 30, 40, 50}; for(int x : numbers ) { if( x == 30 ) { continue; } System.out.print( x ); System.out.print("\n"); } } }
以上實例編譯運行結果如下:
10 20 40 50