循环语句就是让计算机根据条件做循环计算,在条件满足时继续循环,条件不满足时退出循环。

例如,计算从1到100的和:

1
1 + 2 + 3 + 4 + … + 100 = ?

除了用数列公式外,完全可以让计算机做100次循环累加。因为计算机的特点是计算速度非常快,我们让计算机循环一亿次也用不到1秒,所以很多计算的任务,人去算是算不了的,但是计算机算,使用循环这种简单粗暴的方法就可以快速得到结果。

我们先看Java提供的while条件循环。它的基本用法是:

1
2
3
4
while (条件表达式) {
循环语句
}
// 继续执行后续代码

while循环在每次循环开始前,首先判断条件是否成立。如果计算结果为true,就把循环体内的语句执行一遍,如果计算结果为false,那就直接跳到while循环的末尾,继续往下执行。

我们用while循环来累加1到100,可以这么写:

1
2
3
4
5
6
7
8
9
10
11
12
13
// while

public class Main {
public static void main(String[] args) {
int sum = 0; // 累加的和,初始化为0
int n = 1;
while (n <= 100) { // 循环条件是n <= 100
sum = sum + n; // 把n累加到sum中
n ++; // n自身加1
}
System.out.println(sum); // 5050
}
}

注意到while循环是先判断循环条件,再循环,因此,有可能一次循环都不做。

对于循环条件判断,以及自增变量的处理,要特别注意边界条件。思考一下下面的代码为何没有获得正确结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
// while

public class Main {
public static void main(String[] args) {
int sum = 0;
int n = 0;
while (n <= 100) {
n ++; // 最后一次循环,此处n自加变成了101
sum = sum + n;
}
System.out.println(sum);
}
}

如果循环条件永远满足,那这个循环就变成了死循环。死循环将导致100%的CPU占用,用户会感觉电脑运行缓慢,所以要避免编写死循环代码。

如果循环条件的逻辑写得有问题,也会造成意料之外的结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// while

public class Main {
public static void main(String[] args) {
int sum = 0;
int n = 1;
while (n > 0) {
sum = sum + n;
n ++;
}
System.out.println(n); // -2147483648
System.out.println(sum);
}
}

表面上看,上面的while循环是一个死循环,但是,Java的int类型有最大值,达到最大值后,再加1会变成负数,结果,意外退出了while循环。

练习

使用while计算从mn的和:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
* while练习
*/
public class Main {

public static void main(String[] args) {
int sum = 0;
int m = 20;
int n = 100;
// FIXME: 使用while计算M+...+N:
while (m <= n) {
sum += m;
m++;
}
System.out.println(sum);
}

}

小结

while循环先判断循环条件是否满足,再执行循环语句;

while循环可能一次都不执行;

编写循环时要注意循环条件,并避免死循环。

评论