之前的文章中給大家介紹了選擇結(jié)構(gòu)來控制程序流轉(zhuǎn),這篇文章內(nèi)容著重介紹一下另外一種程序流控制,循環(huán)結(jié)構(gòu)。
有的時候我們需要在程序中重復(fù)的去做某一件事情,比如重復(fù)對某個變量進行相同的操作。這個時候我們就可以采用循環(huán)結(jié)構(gòu)來幫我們處理。
在 Java 中常用的循環(huán)結(jié)構(gòu)主要三種:
-
for
循環(huán) -
while
循環(huán) -
do while
循環(huán)
1. for 循環(huán)
for 循環(huán)是經(jīng)常用到的一種循環(huán)結(jié)構(gòu),它的結(jié)構(gòu)是這樣的
for (循環(huán)變量初始值; 判斷循環(huán)是否結(jié)束的表達式; 更新循環(huán)控制變量) {
循環(huán)內(nèi)容
}
for
循環(huán)處理一個數(shù)學(xué)計算題:
從 1 加到 100 的值是多少?
int num = 0;
for (int i = 0; i <= 100; i++) {
num += i;
}
System.out.println("num = " + num);
- 執(zhí)行初始化步驟,
int i = 0
; - 判斷循環(huán)是否結(jié)束,
i < = 100
如果為true,循環(huán)體num += 1
被執(zhí)行。如果為false,循環(huán)終止 - 執(zhí)行一次循環(huán)后,更新循環(huán)控制變量
i++
- 再次判斷循環(huán)是否結(jié)束,循環(huán)執(zhí)行上面的過程
注意不要死循環(huán):
for (;;;) {
System.out.println("這是一個死循環(huán),會一直執(zhí)行下去");
}
for
循環(huán)來遍歷輸出數(shù)組的值:
輸出顏色數(shù)組 {"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
循環(huán):
String[] colors = {"red", "yellow", "blue", "green","orange"};
for (String color : colors) {
System.out.println(color);
}
2. while 循環(huán)
for
循環(huán)我們可以很輕易的次數(shù),循環(huán)次數(shù)達到會結(jié)束循環(huán)。while
循環(huán)則會一種不斷地運行,直到指定的條件不滿足位置。
while
循環(huán)的結(jié)構(gòu):
while (布爾判斷表達式) {
循環(huán)內(nèi)容
}
while
循環(huán)處理數(shù)學(xué)問題:
從 1 加到 100 的值是多少?
int i = 1;
int sum = 0;
while (i <= 100) {
sum += i;
i++;
}
System.out.println("sum = " + sum);
while
循環(huán)將布爾判斷表達式:i <= 100
的值控制循環(huán)是否結(jié)束,i <= 100
為 false
終止循環(huán)。
死循環(huán)是這樣子的:
while (true) {
System.out.println("這是死循環(huán),會一直執(zhí)行");
}
擴展 while
循環(huán)應(yīng)用
需求:持續(xù)從鍵盤獲取輸入內(nèi)容,并打印出來
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 可以跳出整個循環(huán)
3. do while 循環(huán)
對于while
語句而言,如果不滿足條件,則不能進入循環(huán)。但有時候我們需要即使不滿足條件,也至少執(zhí)行一次。 do while
循環(huán)和while
循環(huán)相似,不同的是,do while
循環(huán)至少會執(zhí)行一次。
do while 循環(huán)的結(jié)構(gòu):
do {
循環(huán)的內(nèi)容
} while (布爾表達式)
再來解決一下這個數(shù)據(jù)問題:從 1 加到 100 的值是多少?
int i = 0;
int sum = 0;
do {
sum += i;
i++;
} while (i <= 100);
System.out.println("sum = " + sum);
4. break 的作用
我們已經(jīng)知道break
可以跳出 switch
結(jié)構(gòu),在循環(huán)結(jié)構(gòu)中,它可以跳出整個循環(huán)。
持續(xù)從鍵盤獲取輸入內(nèi)容,并打印出來的實例中已經(jīng)知道 break 可以跳出 while
循環(huán),對于 for
循環(huán),它也有同樣的作用。
String[] colors = {"red", "yellow", "blue", "green","orange"};
for (String color : colors) {
System.out.println(color);
if ("blue".equals(color)) {
break;
}
}
5. continue 的作用
continue
用于循環(huán)結(jié)構(gòu)中,可以讓程序立刻跳到下一次循環(huán)。
輸出 1 到 100 之內(nèi)的奇數(shù):
for (int i = 0; i <= 100; i++) {
if (i % 2 == 0) {
continue;
}
System.out.println(i);
}
6. return 的作用
-
return
后面跟一個值或者變量,指定一個方法的返回值 - 單獨的
return;
語句,后面不跟任何值或者變量,表示退出當前方法
sum
函數(shù)返回計算結(jié)果給調(diào)用該方法者
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);
}