程序中的循環流程
程序中的循環流程
-
在控制檯中輸出100個Hello字符串。
-
在控制檯中輸出從 1 到 100 的數字。
-
… …
-
循環變量 (循環時使用的變量)
-
循環體 (不斷執行的語句)
-
循環條件 (判斷是否繼續循環)
-
while循環語句
-
do…while循環語句
-
for循環語句
while循環語句使用while關鍵字實現程序循環流程:
int count = 1;
while (count<=100) {
System.out.println(count);
count++;
}複製代碼
int count = 1;
while (count<=100) {
if(count==80){
break;
}
System.out.println(count);
count++;
}複製代碼
每當count是3的倍數時,不輸出count的值:
int count = 1;
while (count<=100) {
if(count%3 == 0){
continue;
}
System.out.println(count);
count++;
}複製代碼
do-while關鍵字實現的循環流程與while循環的區別在於do-while會先執行一次循環體,而後再判斷是否應該繼續重複執行循環體。
int count = 1;
do {
System.out.println(count);
count++;
} while(count<=100);複製代碼
for循環語句的句式最複雜,但對於執行固定次數的循環時卻很是實用:
課後做業
1.99乘法表
2.排數字
有一、二、三、4個數字,能組成多少個互不相同且無重複數字的三位數?都是多少?
能夠將答案代碼,寫入留言區,代碼要不斷的編寫纔會培養「碼感」的呦!你不關注一下嗎?