1. 程式人生 > >Java--流程控制之迴圈語句

Java--流程控制之迴圈語句

前言

迴圈語句可以在滿足迴圈條件的情況下,反覆執行某一段程式碼,這段被重複執行的程式碼被稱為迴圈體語句,當反覆執行這個迴圈體時,需要在合適的時候把迴圈判斷條件修改為false,從而結束迴圈,否則迴圈將一直執行下去,形成死迴圈。

內容

1. for – 迴圈語句

語句格式:

for(初始化表示式①; 布林表示式②; 步進表示式④){
迴圈體③        
}

執行流程:

  • 執行順序:①②③④>②③④>②③④…②不滿足為止。
  • ①負責完成迴圈變數初始化
  • ②負責判斷是否滿足迴圈條件,不滿足則跳出迴圈
  • ③具體執行的語句
  • ④迴圈後,迴圈條件所涉及變數的變化情況 在這裡插入圖片描述 程式碼展示:
public
static void main(String[] args) { //控制檯輸出10次HelloWorld,不使用迴圈 System.out.println("HelloWorld"); System.out.println("HelloWorld"); System.out.println("HelloWorld"); System.out.println("HelloWorld"); System.out.println("HelloWorld"); System.out.println("HelloWorld"); System.
out.println("HelloWorld"); System.out.println("HelloWorld"); System.out.println("HelloWorld"); System.out.println("HelloWorld"); System.out.println("‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐"); //用迴圈改進,迴圈10次 //定義變數從0開始,迴圈條件為<10 for(int x = 0; x < 10; x++) { System.out.println("HelloWorld"
+x); } }

1.1 舉個例子

使用迴圈,打印出 a-z 26個小寫字母

/* 
  迴圈練習:
        使用迴圈,打印出 a‐z 26個小寫字母
    分析:
        a‐z:其實是對應ASCII中的97‐122連續的26個數字
    實現步驟
        1.使用for迴圈得到97‐122這些數字
        2.輸出每個數字
*/
public static void main(String[] args) {
	 // 迴圈拿到97‐122連續的26個數字    
	 for (int i = 97; i < 123; i++) {    
	 // 列印的時候轉成char型別列印,就能看到字元        
	 System.out.println((char)i);        
	 }    
}

使用迴圈,計算1-5的和 注意:

  • 不要將sum定義在for迴圈裡面
  • 思考:計算12 - 23之間的資料的和
/*
       迴圈練習:
           迴圈練習:使用迴圈,計算1‐5的和
       分析:
           1.需要拿到1‐5之間的這些數字
           2.將這些數相加起來
       實現步驟:
           1.定義求和變數,初始化值是0
           2.獲取1‐5之間的資料,用for迴圈實現
           3.把每一次獲取到的資料,累加起來
           4.輸出求和變數
*/
 public static void main(String[] args) {
   // 需要拿到1‐5之間的這些數字
   /* for (int i = 1; i <= 5; i++) {
       System.out.println(i);
   } */
   // 用來儲存這些數的和
   int sum = 0;
   for (int i = 1; i <= 5; i++) {
       sum = sum + i;
       /*
           i = 1; sum = 1; 
           i = 2; sum = 1 + 2; 
           i = 3; sum = 1 + 2 + 3; 
           i = 4; sum = 1 + 2 + 3 + 4; 
           i = 5; sum = 1 + 2 + 3 + 4 + 5; 
                    */
   }
   System.out.println("總和: " + sum);
 }

使用迴圈,計算1-100之間的偶數和

  • 思考:有沒有其他方法獲取偶數呢?
/*
	迴圈練習:    
	使用迴圈,計算1‐100之間的偶數和        
	       
	分析:    
	1.需要拿到1‐100之間的這些數字        
	2.需要拿到1‐100之間的這些偶數        
	3.將這些偶數相加起來        
	       
	實現步驟    
	1.定義求和變數        
	2.使用for迴圈得到1‐100的數字        
	3.i % 2 == 0 判斷是否是偶數        
	4.將偶數求和        
*/
public class Demo04for {
	public static void main(String[] args) {    
	// 拿到1‐100之間的這些數字        
	/* for (int i = 1; i <= 100; i++) {        
	System.out.println(i);            
	} */        
	       
	// 拿到1‐100之間的這些偶數        
	/* for (int i = 1; i <= 100; i++) {        
	if (i % 2 == 0) {            
	System.out.println(i);                
	}            
	} */        	       
		// 定義求和變數        
		int sum = 0;         
		// 使用for迴圈得到1‐100的數字        
		for (int i = 1; i <= 100; i++) {        
			// 判斷是否是偶數            
			if (i % 2 == 0) {            
				// 將偶數求和                
				sum = sum + i;                
			}            
		}               
	System.out.println("總和: " + sum);        
	}    
}

2. while – 迴圈語句

語句格式:

初始化表示式①
while(布林表示式②) {
    迴圈體③
    步進表示式④

執行流程:

  • 執行順序:①②③④>②③④>②③④…②不滿足為止。
  • ①負責完成迴圈變數初始化。
  • ②負責判斷是否滿足迴圈條件,不滿足則跳出迴圈。
  • ③具體執行的語句。
  • ④迴圈後,迴圈變數的變化情況。 在這裡插入圖片描述

2.1 舉個例子

while迴圈輸出10次HelloWorld

public static void main(String[] args) {
    //while迴圈實現列印10次HelloWorld
    //定義初始化變數
    int i = 1;
    //迴圈條件<=10
    while(i<=10) {
        System.out.println("HelloWorld");
        //步進
        i++;
    }
}

while迴圈計算1-100之間的和

/*
練習:while迴圈計算1‐100之間的和    
   
實現步驟:    
1.定義求和變數sum,初始化值是0        
    2.獲取1‐100之間的資料,用while迴圈實現    
    3.把每一次獲取到的資料累加起來    
    4.輸出求和變數    
*/
public class Demo02while {
	public static void main(String[] args) {    
		// 1.定義求和變數sum,初始化值是0        
		int sum = 0;        
		    // 2.獲取1‐100之間的資料,用while迴圈實現    
		int i = 1;        
		while (i <= 100) {        
			// 3.把每一次獲取到的資料累加起來            
			sum += i;            
			i++;            
		}        
		       
		    // 4.輸出求和變數    
		System.out.println("總和: " + sum);        
	}    
}

3. do - while – 迴圈語句

語句格式:

初始化表示式①
    do {
    迴圈體③
    步進表示式④
} while(布林表示式②);

執行流程:

  • 執行順序:①③④>②③④>②③④…②不滿足為止。
  • ①負責完成迴圈變數初始化。
  • ②負責判斷是否滿足迴圈條件,不滿足則跳出迴圈。
  • ③具體執行的語句
  • ④迴圈後,迴圈變數的變化情況 在這裡插入圖片描述

舉個例子

輸出10次HelloWorld

public static void main(String[] args) {
    int x=1;
    do {
      System.out.println("HelloWorld");
      x++;
    } while(x<=10);
}

do - while迴圈的特點: 無條件執行一次迴圈體,即使我們將迴圈條件直接寫成false,也依然會迴圈一次。這樣的迴圈具有一定的風險性,因此初學者不建議使用do…while迴圈。 eg.

public static void main(String[] args) {
    do {
       System.out.println("無條件執行一次");  
    } while(false);
}

迴圈語句的區別

for 和 while 的小區別:

  • 控制條件語句所控制的那個變數,在for迴圈結束後,就不能再被訪問到了,而while迴圈結束還可以繼續使用,如果你想繼續使用,就用while,否則推薦使用for。原因是for迴圈結束,該變數就從記憶體中消失,能夠提高記憶體的使用效率。
  • 在已知迴圈次數的時候使用推薦使用for,迴圈次數未知的時推薦使用while。

離開語句

break 我們使用 for迴圈 和 while迴圈 可以讓程式重複執行程式碼.但是有時候我們可能在迴圈的中途讓迴圈停下來.需要使用 break eg. 使用場景:跳出迴圈,讓迴圈提前結束

  • 在選擇結構switch語句中
  • 在迴圈語句中
  • 離開使用場景的存在是沒有意義的
public static void main(String[] args) {
    for (int i = 1; i<=10; i++) {
        //需求:列印完兩次HelloWorld之後結束迴圈
        if(i == 3) {
          break; // 結束迴圈
        }
        System.out.println("HelloWorld" + i);
    }
}

continue 使用場景:結束本次迴圈,繼續下一次的迴圈

/*
continue:結束本次迴圈,繼續下次迴圈    
必須放在for迴圈或while迴圈中        
   
在聚會我我們常會玩數字遊戲,比如大家圍成一圈報數,    
但是報到3的倍數的跳過不能說,下一個人結束報下一個數    
*/
public class Demo02continue {
	public static void main(String[] args) { 
		for (int i = 1; i < 10; i++) {        ++) {        
			if (i == 3) {            
				continue; // continue後面的程式碼不執行.繼續下次迴圈                
			}            
		System.out.println("HelloWorld" + i);            
		}        
	}    
}

總結

在不少實際問題中有許多具有規律性的重複操作,因此在程式中就需要重複執行某些語句。一組被重複執行的語句稱之為迴圈體,能否繼續重複,決定迴圈的終止條件。迴圈結構是在一定條件下反覆執行某段程式的流程結構,被反覆執行的程式被稱為迴圈體。迴圈語句是由迴圈體及迴圈的終止條件兩部分組成的。

end

謝謝您的閱讀!