跳转至

循环语句

学习编程语言语法是次要的,思维是主要的。如何把头脑中的想法变成简洁的代码,至关重要。

学习循环语句只需要抓住一点——代码执行顺序!

一、while循环

可以简单理解为循环版的if语句。if语句是判断一次,如果条件成立,则执行后面的语句;while是每次判断,如果成立,则执行循环体中的语句,否则停止。

1
2
3
4
5
6
7
8
9
public class Main {
    public static void main(String[] args) {
        int i = 0;
        while (i < 10) {
            System.out.println(i);
            i ++ ;
        }
    }
}

练习:求 1~100 中所有数的立方和。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public class Main {
    public static void main(String[] args) {
        int i = 1, sum = 0;
        while (i <= 100) {
            sum += i * i * i;
            i ++ ;
        }
        System.out.println(sum);
    }
}

练习:求斐波那契数列的第n项。f(1) = 1, f(2) = 1, f(3) = 2, f(n) = f(n-1) + f(n-2)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        int a = 1, b = 1, i = 1;
        while (i < n) {
            int c = a + b;
            a = b;
            b = c;
            i ++ ;
        }

        System.out.println(a);
    }
}

死循环:循环永久执行,无法结束。我们要避免写出死循环。

1
2
3
4
5
6
7
public class Main {
    public static void main(String[] args) {
        int x = 1;
        while (x == 1)
            System.out.println("!");
    }
}

二、do while循环

do while循环不常用。

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
public class Main {
    public static void main(String[] args) {
        int x = 1;
        while (x < 1) {
            System.out.println("x!");
        }

        int y = 1;
        do {
            System.out.println("y!");
        } while (y < 1);
    }
}

三、for循环

基本思想:把控制循环次数的变量从循环体中剥离。

1
2
3
for (init-statement : condition: expression) {
    statement
}

init-statement可以是声明语句、表达式、空语句,一般用来初始化循环变量;

condition是条件表达式,和while中的条件表达式作用一样;可以为空,空语句表示true

expression一般负责修改循环变量,可以为空。

1
2
3
4
5
6
7
public class Main {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i ++ ) {  // 循环体中只有一条语句时,可以不加大括号
            System.out.println(i);
        }
    }
}

练习:求 1~100 中所有数的立方和。

1
2
3
4
5
6
7
8
public class Main {
    public static void main(String[] args) {
        int sum = 0;
        for (int i = 1; i <= 100; i ++ )
            sum += i * i * i;
        System.out.println(sum);
    }
}

练习:求斐波那契数列的第n项。f(1) = 1, f(2) = 1, f(3) = 2, f(n) = f(n-1) + f(n-2)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        int a = 1, b = 1;
        for (int i = 1; i < n; i ++ ) {
            int c = a + b;
            a = b;
            b = c;
        }

        System.out.println(a);
    }
}

init-statement可以定义多个变量,expression也可以修改多个变量。

例如求 1 * 10 + 2 * 9 + 3 * 8 + 4 * 7 + 5 * 6

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public class Main {
    public static void main(String[] args) {
        int sum = 0;
        for (int i = 1, j = 10; i < j; i ++, j -- ) {
            sum += i * j;
        }

        System.out.println(sum);
    }
}

四、跳转语句

1. break

可以提前从循环中退出,一般与if语句搭配。

例题:判断一个大于 1 的数是否是质数:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        boolean isPrime = true;
        for (int i = 2; i < n; i ++ )
            if (n % i == 0) {
                isPrime = false;
                break;
            }

        if (isPrime)
            System.out.println("yes");
        else
            System.out.println("no");
    }
}

2. continue

可以直接跳到当前循环体的结尾。作用与if语句类似。

例题:求 1~100 中所有偶数的和。


五、多层循环

将 1~100 打印到一个 10 * 10 的矩阵中:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public class Main {
    public static void main(String[] args) {
        for (int i = 0, k = 1; i < 10; i ++ ) {
            for (int j = 0; j < 10; j ++, k ++ ) {
                System.out.printf("%d ", k);
            }
            System.out.println();
        }
    }
}

练习:打印 1~100 中的所有质数

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
public class Main {
    public static void main(String[] args) {
        for (int i = 2; i <= 100; i ++ ) {
            boolean isPrime = true;
            for (int j = 2; j < i; j ++ ) {
                if (i % j == 0) {
                    isPrime = false;
                    break;
                }
            }
            if (isPrime)
                System.out.println(i);
        }
    }
}

最后更新: 2023-02-28 16:01:02

评论