循环

编程语言 / loop

本地源文件:docs/lang__loop.md

循环

有时,我们需要做一件事很多遍,为了不写过多重复的代码,我们需要循环.

有时,循环的次数不是一个常量,那么我们无法将代码重复多遍,必须使用循环.

for 语句

以下是 for 语句的结构:

---|---

执行顺序:

![](./images/for-loop.svg)

e.g. 读入 n 个数:

---|---

for 语句的三个部分中,任何一个部分都可以省略.其中,若省略了判断条件,相当于判断条件永远为真.

while 语句

以下是 while 语句的结构:

---|---

执行顺序:

![](./images/while-loop.svg)

e.g. 验证 3x+1 猜想:

---|---

do...while 语句

以下是 do...while 语句的结构:

---|---

执行顺序:

![](./images/do-while-loop.svg)

与 while 语句的区别在于,do...while 语句是先执行循环体再进行判断的.

e.g. 枚举排列:

---|---

三种语句的联系

---|---

在 statement4 中没有 `continue` 语句(见下文)的时候是等价的,但是下面一种方法很少用到.

---|---

在 statement1 中没有 continue 语句的时候这两种方式也也是等价的.

---|---

这两种方式都是永远循环下去.(可以使用 `break`(见下文)退出.)

可以看出,三种语句可以彼此代替,但一般来说,语句的选用遵守以下原则:

  1. 循环过程中有个固定的增加步骤(最常见的是枚举)时,使用 for 语句;
  2. 只确定循环的终止条件时,使用 while 语句;
  3. 使用 while 语句时,若要先执行循环体再进行判断,使用 do...while 语句.一般很少用到,常用场景是用户输入.

## break 与 continue 语句

break 语句的作用是退出循环.

continue 语句的作用是跳过循环体的余下部分.下面以 continue 语句在 do...while 语句中的使用为例:

---|---

break 与 continue 语句均可在三种循环语句的循环体中使用.

一般来说,break 与 continue 语句用于让代码的逻辑更加清晰,例如:

---|---

---|---

---|---

* * *

> __本页面最近更新: 2026/1/7 08:56:54,[更新历史](https://github.com/OI-wiki/OI-wiki/commits/master/docs/lang/loop.md)
>  __发现错误?想一起完善?[在 GitHub 上编辑此页!](https://oi-wiki.org/edit-landing/?ref=/lang/loop.md "edit.link.title")
>  __本页面贡献者:[Ir1d](https://github.com/Ir1d), [ouuan](https://github.com/ouuan), [orzAtalod](https://github.com/orzAtalod), [CCXXXI](https://github.com/CCXXXI), [Enter-tainer](https://github.com/Enter-tainer), [Great-designer](https://github.com/Great-designer), [mcendu](https://github.com/mcendu), [Tiphereth-A](https://github.com/Tiphereth-A), [tLLWtG](https://github.com/tLLWtG)
>  __本页面的全部内容在**[CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/deed.zh) 和 [SATA](https://github.com/zTrix/sata-license)** 协议之条款下提供,附加条款亦可能应用