# while 循环

while 循环是命令式编程语言中的一种控制流语句，只要测试条件求值为真，就重复执行一段代码体；当条件变为假时，执行继续沿循环之后的语句进行<sup>[1](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while)</sup>。它与顺序和选择并列为结构化编程的经典迭代构造之一<sup>[2](https://www.codehistory.org/software-engineering-tools-version-control/structured-programming-dijkstra-goto-history/)</sup>。

| 关键事实 | 详情 |
|---|---|
| 语义 | 求值条件；若为真，执行循环体，然后将控制转回条件处重新求值；若为假，退出循环<sup>[3](https://cse.buffalo.edu/~bina/cs113/spring98/lec8/tsld006.htm)</sup><sup> • </sup><sup>[1](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while)</sup> |
| 测试位置 | 条件在每次迭代之前测试，因此循环体可能一次也不执行<sup>[1](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while)</sup> |
| 结构化编程角色 | 迭代（while/for）是三大构件之一，与顺序和选择共同构成结构化程序<sup>[2](https://www.codehistory.org/software-engineering-tools-version-control/structured-programming-dijkstra-goto-history/)</sup> |
| 表达力 | 任何非结构化程序，等价地任何流程图，都等价于一个只含单个 while 循环外加一个额外整型变量的结构化程序<sup>[4](https://xavierleroy.org/control-structures/book/main005.html)</sup> |
| 形式验证 | 按迪杰斯特拉的说法，设计重复构造需要发明一个不变式关系和一个变体函数<sup>[5](https://www.cs.utexas.edu/~EWD/transcriptions/EWD04xx/EWD418.html)</sup> |
| 语言中的存在 | 在《Java 语言规范》（第 14.12 节）、D 语言规范、JavaScript、Python、C、C++ 和 C# 中均有定义<sup>[6](https://docs.oracle.com/javase/specs/jls/se15/jls15.pdf)</sup><sup> • </sup><sup>[7](https://dlang.org/spec/statement.html)</sup> |

## 语义

while 循环遵循一个固定的循环过程。首先求值条件；若为真，则执行循环体，随后控制转回条件处重新求值<sup>[3](https://cse.buffalo.edu/~bina/cs113/spring98/lec8/tsld006.htm)</sup>。此过程重复进行，直到条件求值为假，此时执行继续沿循环之后的语句进行<sup>[1](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while)</sup>。

由于测试先于循环体，while 循环是一种入口控制结构：若首次求值时条件为假，循环体便从不执行。

条件是一个求值为布尔值的表达式。循环体中需要多条语句时，可以使用块语句<sup>[1](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while)</sup>。

## 在结构化编程中的地位

在结构化编程中，程序的清晰性开始被当作工程问题来对待：其论点是，控制流应当由可理解的结构搭建而成，而非任意的跳转。<sup>[2](https://www.codehistory.org/software-engineering-tools-version-control/structured-programming-dijkstra-goto-history/)</sup> 创造“结构化编程”一词的荷兰计算机科学家艾兹格·W·迪杰斯特拉（Edsger W. Dijkstra）主张，程序员需要在其静态的程序文本与其所创造的动态过程之间建立一种可管理的对应关系<sup>[2](https://www.codehistory.org/software-engineering-tools-version-control/structured-programming-dijkstra-goto-history/)</sup>。如果一个程序将自身限制为顺序组合、经由 if/else 的选择，以及经由 while/for 的迭代，那么它的控制流便与其在文本中的排布方式相对应，从而可以通过从上到下阅读来理解程序<sup>[8](https://github.com/akardapolov/software-atlas/blob/main/docs/works/papers/dijkstra-1968-goto.md)</sup>。

其理论基础是与伯姆（Böhm）和雅科皮尼（Jacopini）1966 年证明相关的结构化程序定理：任何可计算函数只需要顺序、选择和迭代<sup>[9](https://thingshavehistory.com/posts/structured-programming-1972/)</sup>。同样的思想可以表述得更为精确：给定一个非结构化程序——或者换个说法，一个流程图——人们可以用仅一个 while 循环外加一个额外的整型变量来构造一个与之等价的结构化程序。<sup>[4](https://xavierleroy.org/control-structures/book/main005.html)</sup>

迪杰斯特拉 1968 年的信件以“A Case against the GO TO Statement”为题投稿，由《ACM 通讯》编辑尼克劳斯·维尔特（Nikolaus Wirth）以改定的标题“Go To Statement Considered Harmful”发表，使这一论点广为人知<sup>[2](https://www.codehistory.org/software-engineering-tools-version-control/structured-programming-dijkstra-goto-history/)</sup>。他所针对的并非从机器码中消除底层跳转指令的问题，而是高层语言是否应当将任意跳转作为一种常规的程序构造机制暴露出来的问题<sup>[2](https://www.codehistory.org/software-engineering-tools-version-control/structured-programming-dijkstra-goto-history/)</sup>。

## 关于循环的推理

迪杰斯特拉引入了文本索引的概念，即读者在源代码中的位置，作为理解程序执行时所处位置的坐标。对于循环，定位执行状态需要行号加上循环变量，因为同一段文本会被多次遍历<sup>[8](https://github.com/akardapolov/software-atlas/blob/main/docs/works/papers/dijkstra-1968-goto.md)</sup>。

正因如此，循环的正确性才通过不变式加以形式化处理。在其 1974 年关于卫式命令与程序形式推导的论文中，迪杰斯特拉写道，选择构造（if 语句）的设计看来相当直接，而重复构造的设计则需要“发明”一个不变式关系和一个变体函数——他认为这是一种发明<sup>[5](https://www.cs.utexas.edu/~EWD/transcriptions/EWD04xx/EWD418.html)</sup>。不变式是在每次迭代前后都成立的性质，因而在循环出口处也成立，在那里它可以与假条件结合以确立循环的结果；变体函数界定剩余迭代的次数，从而确立终止性。在迪杰斯特拉的形式化循环推导方法中，人们先选择一个不变式关系和一个变体函数，然后推导出循环结构及其语句，迪杰斯特拉以其卫式命令语言强化了这一重点<sup>[10](https://vanemden.wordpress.com/2018/10/29/history-of-structured-programming/)</sup><sup> • </sup><sup>[5](https://www.cs.utexas.edu/~EWD/transcriptions/EWD04xx/EWD418.html)</sup>。

## 语言形式与变体

while 语句出现在各主流语言中，语义高度一致。《Java 语言规范》在“块与语句”部分的第 14.12 节对其作了定义<sup>[6](https://docs.oracle.com/javase/specs/jls/se15/jls15.pdf)</sup>。D 语言规范将 while 语句描述为实现简单循环<sup>[7](https://dlang.org/spec/statement.html)</sup>。JavaScript 的 while 语句创建一个循环，只要测试条件求值为真，就执行指定的语句<sup>[1](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while)</sup>。在 C、C++、C# 和 Java 中，其语法遵循 C 的传统：带括号的条件和一个受控语句<sup>[11](https://jhzhang.cn/resources/A050113G/Ch6.pdf)</sup>。

有两种辅助语句可改变循环的执行。break 语句立即退出所在循环，continue 语句停止当前循环体的执行并重新求值条件<sup>[1](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while)</sup>。各语言限制这些语句可以出现的位置：在 Python 中，continue 在语法上只能嵌套于 for 或 while 循环之内，且不得嵌套于该循环内的函数或类定义之中<sup>[12](https://github.com/python/cpython/blob/main/Doc/reference/simple_stmts.rst)</sup>。

while 循环与计数式 for 循环在原则上大体可以互换，但它们适合不同的任务。

## 语言理论与编译中的循环

一种围绕 while 构造构建的简化命令式语言，径直称为 While 语言，是教学和开发形式化方法的标准载体：学习者通过亲手规定其语法、操作语义和类型系统，练习编写形式化规约<sup>[13](https://arxiv.org/abs/1603.08949)</sup>。编译器验证研究同样在 While 语言中对源程序建模；一个用证明助手 Isabelle/HOL 编写并执行的经过验证的编译器，将带有共享内存并发性的命令式 While 语言的信息流安全性一路传递到 RISC 风格的汇编模型<sup>[14](https://arxiv.org/abs/1907.00713)</sup>。

编译器也会对循环进行变换。[推测执行](https://www.edgepedia.org/zh/speculative-execution)技术可以在完成循环条件求值之前就执行 while 循环的循环体，若条件的值不符合假设则回滚；在实时系统中，此类变换必须谨慎应用，以免回滚延长最坏情况执行时间<sup>[15](https://userpages.cs.umbc.edu/younis/Publications/Automatica/automatica.pdf)</sup>。

## References

1. [while - JavaScript, MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while)
2. [Structured Programming and Dijkstra's Campaign Against Uncontrolled Jumps, CodeHistory](https://www.codehistory.org/software-engineering-tools-version-control/structured-programming-dijkstra-goto-history/)
3. [while loop: semantics, University at Buffalo CS 113 lecture notes](https://cse.buffalo.edu/~bina/cs113/spring98/lec8/tsld006.htm)
4. [Chapter 2, Structured programming, Xavier Leroy](https://xavierleroy.org/control-structures/book/main005.html)
5. [EWD 418: Guarded commands, non-determinacy and a calculus for the derivation of programs, E.W. Dijkstra Archive, University of Texas at Austin](https://www.cs.utexas.edu/~EWD/transcriptions/EWD04xx/EWD418.html)
6. [The Java Language Specification, Java SE 15, section 14.12](https://docs.oracle.com/javase/specs/jls/se15/jls15.pdf)
7. [Statements, D Programming Language specification](https://dlang.org/spec/statement.html)
8. [Dijkstra's 1968 GOTO letter, software-atlas documentation, GitHub](https://github.com/akardapolov/software-atlas/blob/main/docs/works/papers/dijkstra-1968-goto.md)
9. [Structured programming: sequence, selection, iteration, Things Have History](https://thingshavehistory.com/posts/structured-programming-1972/)
10. [History of "Structured Programming", A Programmers Place](https://vanemden.wordpress.com/2018/10/29/history-of-structured-programming/)
11. [While loops in Python, C, C++, C#, and Java, lecture notes of Jianhui Zhang](https://jhzhang.cn/resources/A050113G/Ch6.pdf)
12. [Python reference: simple statements, CPython documentation](https://github.com/python/cpython/blob/main/Doc/reference/simple_stmts.rst)
13. [The While language, arXiv](https://arxiv.org/abs/1603.08949)
14. [Verifying that a compiler preserves concurrent value-dependent information-flow security, arXiv](https://arxiv.org/abs/1907.00713)
15. [Control-flow transformations for safe speculative execution in real-time systems, University of Maryland Baltimore County](https://userpages.cs.umbc.edu/younis/Publications/Automatica/automatica.pdf)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Programming languages › Programming language concepts

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/while-loop

English original: https://www.edgepedia.org/while-loop
