10. 程序流控制-循環結構 【連載 10】

10. 程序流控制-循環結構 【連載 10】

之前的文章中給大家介紹了選擇結構來控制程序流轉,這篇文章內容着重介紹一下另外一種程序流控制,循環結構。

有的時候我們需要在程序中重複的去做某一件事情,比如重複對某個變量進行相同的操作。這個時候我們就可以採用循環結構來幫我們處理。

在 Java 中常用的循環結構主要三種:

  1. for 循環
  2. while 循環
  3. do while 循環

1. for 循環

for 循環是經常用到的一種循環結構,它的結構是這樣的

for (循環變量初始值; 判斷循環是否結束的表達式; 更新循環控制變量) {
 循環內容
}

for 循環處理一個數學計算題:

從 1 加到 100 的值是多少?

int num = 0;
for (int i = 0; i <= 100; i++) {
  num += i;
}
System.out.println("num = " + num);
  1. 執行初始化步驟, int i = 0;
  2. 判斷循環是否結束,i &lt; = 100 如果爲true,循環體 num += 1被執行。如果爲false,循環終止
  3. 執行一次循環後,更新循環控制變量 i++
  4. 再次判斷循環是否結束,循環執行上面的過程

注意不要死循環

for (;;;) {
  System.out.println("這是一個死循環,會一直執行下去");
}

for循環來遍歷輸出數組的值

輸出顏色數組 {"red", "yellow", "blue", "green","orange"}的每個顏色

String[] colors = {"red", "yellow", "blue", "green","orange"};
for (int i = 0; i < colors.length; i++) {
  System.out.println(colors[i]);
}

增強的 for 循環:

String[] colors = {"red", "yellow", "blue", "green","orange"};
for (String color : colors) {
  System.out.println(color);
}

2. while 循環

for 循環我們可以很輕易的次數,循環次數達到會結束循環。while 循環則會一種不斷地運行,直到指定的條件不滿足位置。

while 循環的結構:

while (布爾判斷表達式) {
 循環內容 
}

while 循環處理數學問題:

從 1 加到 100 的值是多少?

int i = 1;
int sum = 0;
while (i <= 100) {
  sum += i;
  i++;
}
System.out.println("sum = " + sum);

while 循環將布爾判斷表達式:i &lt;= 100的值控制循環是否結束,i &lt;= 100false 終止循環。

死循環是這樣子的:

while (true) {
  System.out.println("這是死循環,會一直執行");
}

擴展 while 循環應用

需求:持續從鍵盤獲取輸入內容,並打印出來

import java.util.Scanner;
public class WhileDemo {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            String cmd = scanner.next();
            System.out.println(cmd);
            if ("exit".equals(cmd)) {
                break;
            }
        }
        scanner.close();
    }
}

break 可以跳出整個循環

3. do while 循環

對於while語句而言,如果不滿足條件,則不能進入循環。但有時候我們需要即使不滿足條件,也至少執行一次。 do while循環和while循環相似,不同的是,do while循環至少會執行一次。

do while 循環的結構:

do {
  循環的內容
} while (布爾表達式)

再來解決一下這個數據問題:從 1 加到 100 的值是多少?

int i = 0;
int sum = 0;
do {
  sum += i;
  i++;
} while (i <= 100);
System.out.println("sum = " + sum);

4. break 的作用

我們已經知道break 可以跳出 switch 結構,在循環結構中,它可以跳出整個循環。

持續從鍵盤獲取輸入內容,並打印出來的實例中已經知道 break 可以跳出 while循環,對於 for 循環,它也有同樣的作用。

String[] colors = {"red", "yellow", "blue", "green","orange"};
for (String color : colors) {
  System.out.println(color);
  if ("blue".equals(color)) {
    break;
  }
}

5. continue 的作用

continue 用於循環結構中,可以讓程序立刻跳到下一次循環。

輸出 1 到 100 之內的奇數:

for (int i = 0; i <= 100; i++) {
  if (i % 2 == 0) {
    continue;
  }
  System.out.println(i);
}

6. return 的作用

  • return後面跟一個值或者變量,指定一個方法的返回值
  • 單獨的return;語句,後面不跟任何值或者變量,表示退出當前方法

sum 函數返回計算結果給調用該方法者

public static void main(String[] args) {
  int n = sum(100, 200);
  System.out.println(n);
}

public static int  sum(int n1, int n2) {
  int sum = n1 + n2;
  return sum;
}

輸出 0 到 49 的每個值:

for (int i = 0; i <= 100; i++) {
  if (i == 50) {
    return;
  }
  System.out.println(i);
}

扣扣交流羣:468439140

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