Featureintro
The while statement
Also called while loop, while True loop, do while in Python, loop until a condition, while else.
A while loop runs its block again and again while its condition is true. It tests the condition before each pass. Python has no do-while loop, so use while True with an if and a break.
- Language
- Python
Synopsis
a, b = 0, 1
result = []
while a < 50:
result.append(a)
a, b = b, a + b
print(result)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
The loop runs while a < 50 is true. It stops when the test is false.
Semantics
r1. The while statement "is used for repeated execution as long as an expression is true". It "repeatedly tests the expression and, if it is true, executes the first suite; if the expression is false (which may be the first time it is tested) the suite of the else clause, if present, is executed and the loop terminates."1 Examples: fib, zero-passes.
r2. "A break statement executed in the first suite terminates the loop without executing the else clause’s suite. A continue statement executed in the first suite skips the rest of the suite and goes back to testing the expression."1 Example: do-while.
r3. "The conditions used in while and if statements can contain any operators, not just comparisons."3 A non-empty container is true, so while items: loops until the container is empty. Example: drain.
r4. Python has no do-while loop. PEP 315 proposed one, and the PEP was rejected. It says: "Users of the language are advised to use the while-True form with an inner if-break when a do-while loop would have been appropriate."2 Example: do-while.
Examples
stack = [1, 2, 3]
while stack:
print("pop", stack.pop())
print("empty:", stack)
pop 3
pop 2
pop 1
empty: []
attempts = iter(["", "x", "yes"])
while True:
answer = next(attempts)
print("got", repr(answer))
if answer == "yes":
break
got ''
got 'x'
got 'yes'
The body runs at least once, and the test is at the end of the body (rule r4).
n = 0
while n > 0:
print("never")
else:
print("condition false at the first test")
condition false at the first test
Pitfalls
A loop whose condition never becomes false does not stop. For example, x = 0.0 followed by while x != 1.0: x += 0.1 runs forever, because the sum of ten 0.1 values is not exactly 1.0:
This code runs but gives a wrong answer on purpose.
x = 0.0
for _ in range(10):
x += 0.1
print(x, x == 1.0)
0.9999999999999999 False
Use a test such as x < 1.0, or count with integers.
Comparison
Go has no while keyword. Its for statement has a simplest form with a single condition, which "specifies the repeated execution of a block as long as a boolean condition evaluates to true. The condition is evaluated before each iteration."4 So for a < b { ... } in Go is the same as while a < b: in Python. In Go, "If the condition is absent, it is equivalent to the boolean value true", so for { ... } is the Go form of while True:.4
References
- The Python Language Reference, Python 3.15, 8.2. The while statement
- PEP 315 - Enhanced While Loop, Abstract; Notice
- The Python Tutorial, 5. Data Structures, 5.7. More on Conditions
- The Go Programming Language Specification, For statements; For statements with single condition
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag 1M5G6R Edit this page History Report an error Markdown