Java入門基礎(二)——循環語句

循環語句

順序結構的程序語句只能被執行一次。如果您想要同樣的操作執行多次,,就需要使用循環結構。
Java中有三種主要的循環結構:

while 循環
do…while 循環
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(聲明語句 : 表達式)
{
   //代碼句子
}

聲明語句

  1. 聲明新的局部變量,該變量的類型必須和數組元素的類型匹配。其作用域限定在循環語句塊,其值與此時數組元素的值相等。
  2. 表達式:表達式是要訪問的數組名,或者是返回值爲數組的方法。
    實例
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 ) {
         // x 等於 30 時跳出循環
         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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章