Lesson 13 of 55
8 mins readPython While Loops (while ... do ... end)
In Plain English
A `while condition do ... end` loop evaluates its condition before executing the loop body and repeats as long as the condition evaluates to true.
Deep Dive: How It Works
Pre-Test Condition: Evaluated before entering the block.
Loop Body: Enclosed between `do` and `end`.
No Increment Operator: Lua has no `++` operator; use `i = i + 1`.
Core Rules to Remember

do ... end Enclosure: Loop blocks start with `do` and terminate with `end`.

i = i + 1: Lua requires explicit assignment `i = i + 1` (no `++`).
Live Interactive Example
Hit Run Code to see it liveCounting with while Loop
Python 3.12
1
2
3
4
5
6
Output Console
Click "Run Code" to view the rendered output.
How it works: Loop executed 5 times, multiplying n by 2 each iteration.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Count Down with while
Declare `local count = 3`.
Use `while count > 0 do print("T-" .. count) count = count - 1 end`.
1
2
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.