Keywords and Control Flow

while — Condition-controlled repetition

Test a condition before each iteration, repeating the body only while it is truthy. The body may run zero times. Update the relevant state or arrange an explicit exit so the loop can terminate. continue retests the condition; break skips the optional completion clause. A false condition normally leads to the else clause, if present.

Formal Syntax & Structure

python syntaxpython
while condition:    suite[else:    suite]

Working Example

Example Snippetpython
remaining = 3while remaining > 0:    print(remaining)    remaining -= 1else:    print("done")
Python Reference