循环语句

while循环

与C++、Python类似,例如:

int i = 0;
while (i < 5) {
   System.out.println(i);
   i ++ ;
}

do while循环

与C++、Python类似,例如:

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

do while语句与while语句非常相似。唯一的区别是,do while语句限制性循环体后检查条件。不管条件的值如何,我们都要至少执行一次循环。

for循环

与C++、Python类似,例如:

for (int i = 0; i < 5; i ++ ) {  // 普通循环
   System.out.println(i);
}

int[] a = {0, 1, 2, 3, 4};
for (int x: a) {  // forEach循环
   System.out.println(x);
}

练习题

偶数 正数

递增序列 约数 菱形


项目分区导航零食 ⬅️ | 00-循环语句 | ➡️ 偶数