Lesson 13 of 55
8 mins readPython While & Until Loops
In Plain English
`while` loops repeat a block as long as the condition evaluates to true. `until` is the clean inverted equivalent, looping until the condition becomes true.
Deep Dive: How It Works
while Loop: Tests condition at the top; continues while truthy.
until Loop: Repeats until condition becomes truthy (equivalent to while not condition).
Infinite Loops: `while (1) { ... }` runs indefinitely until explicitly broken with `last`.
Core Rules to Remember

while Loop: Repeats as long as condition evaluates to true.

until Loop: Repeats until condition evaluates to true.
Live Interactive Example
Hit Run Code to see it liveCounting with while and until
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Output Console
Click "Run Code" to view the rendered output.
How it works: while counted up to 3; until terminated once $countdown equaled 0.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Generate Power Sequence with while
Set `my $val = 1;`
Use a `while ($val <= 8)` loop to print `"$val "` and double `$val *= 2;` each iteration.
Print `"\n"` after the loop.
1
2
3
4
5
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.