1. 程式人生 > >JAVA復習5 Java循環結構 - for, while 及 do...while

JAVA復習5 Java循環結構 - for, while 及 do...while

con dsm key width 限定 一個 bre ava mil

就我所知道的編程語言中都有循環語句: for, while 及 do...while,在這裏要說的就是他們的差別,我不喜歡用語言來說,大家看代碼:coding.............

while和do...while差別:

while:

public class xunhuanTest {
<span style="white-space:pre">	</span>public static void main(String args[]){
<span style="white-space:pre">	</span>      int x = 10;
<span style="white-space:pre">	</span>      //先推斷再循環,推斷x是否小於9,成立循環,不成立不循環
<span style="white-space:pre">	</span>      while( x < 9 ){
<span style="white-space:pre">	</span>         System.out.print("value of x : " + x );
<span style="white-space:pre">	</span>         x++;
<span style="white-space:pre">	</span>         System.out.print("\n");
<span style="white-space:pre">	</span>      }
<span style="white-space:pre">	</span>   }
}
執行結果:

無結果

do...while:

public class xunhuanTest {
	public static void main(String args[]){
	      int x = 10;
	      //先循環在推斷。首先循環一次,輸出value of x : 10,在推斷x是否小於9。成立則循環。不成立不循環
	      do{
	         System.out.print("value of x : " + x );
	         x++;
	         System.out.print("\n");
	      }while( x < 9 );
	   }
}

執行結果:

value of x : 10

從上兩個樣例中。能夠看出 while和do...while差別:

while先推斷再循環。do...while先循環在推斷,即至少運行一次。

在實際編碼操作中。用的最多的還是for循環:

public class xunhuanTest {
	public static int x = 10;

	public static void main(String args[]) {
		// for循環,循環出10-14之間的值(包含10和14)
		for (x = x; x < 15; x++) {
			System.out.print("value of x : " + x);
			System.out.print("\n");
		}
	}
}
public class xunhuanTest {
	public static int x = 10;

	public static void main(String args[]) {
		// for循環,循環出10-14之間的值(包含10和14)
		for (; x < 15; x++) {
			System.out.print("value of x : " + x);
			System.out.print("\n");
		}
	}
}<strong style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </strong>

執行結果都為:

value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14

能夠看出for循環特點:

  • 最先運行初始化步驟。能夠聲明並初始化一個或多個循環控制變量,也能夠是空語句。

  • 然後。檢測布爾表達式的值。假設為true。循環體被運行。假設為false,循環終止,開始運行循環體後面的語句。
  • 運行一次循環後。更新循環控制變量。

  • 再次檢測布爾表達式。循環運行上面的過程。
for循環嵌套:
public class xunhuanTest {
	public static int x = 10;
	public static int y = 10;
	public static void main(String args[]) {
		// for循環。循環出10-14之間的值(包含10和14)
		for (; x < 15; x++) {			
			for(;y<15;y++){
				System.out.print("value of x : " + x);
				System.out.print("value of y : " + y);
				System.out.print("\n");
			}			
		}
	}
}
執行結果: value of x : 10value of y : 10
value of x : 10value of y : 11
value of x : 10value of y : 12
value of x : 10value of y : 13
value of x : 10value of y : 14

Java增強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,
能夠看出:
for(聲明語句 : 表達式)
{
   //代碼句子
}

聲明語句:聲明新的局部變量。該變量的類型必須和數組元素的類型匹配。其作用域限定在循環語句塊,其值與此時數組元素的值相等。

表達式:表達式是要訪問的數組名,或者是返回值為數組的方法。

再來看一下break continue keyword

breakkeyword

break主要用在循環語句或者switch語句中,用來跳出整個語句塊。

break跳出最裏層的循環,而且繼續運行該循環以下的語句。

語法

break的使用方法非常easy,就是循環結構中的一條語句:

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

continuekeyword

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
假設你想了解if語句和ifelse語句請看: http://www.w3cschool.cc/java/java-if-else-switch.html





JAVA復習5 Java循環結構 - for, while 及 do...while