A while loop repeats a block of code as long as a condition evaluates to True. Unlike a for loop, which walks through a known sequence or range, a while loop is the right tool when you cannot say in advance how many repetitions are needed — reading sensor values until a threshold is crossed, asking a user for input until it is valid, or refining a numerical estimate until the error is small enough.
Three ideas hold this topic together. First, the structure: initialize a variable before the loop, test a condition at the top, and update that variable inside the loop body. Second, loop guards: if the condition never becomes False, you get an infinite loop, which is the most common while-loop bug. Third, the comparison with for loops: many loops can be written either way, and recognizing when each is clearer is part of writing readable engineering code.
As you watch, trace the loop variable by hand on each pass — what is its value when the condition is tested, and what changes it inside the body? Pay particular attention to where the update statement sits; moving or forgetting it is usually what turns a working loop into one that never stops.
Khan Academy's introduction to while loops shows how a block of code is repeated while a condition holds, and walks through tracing how a loop variable changes from one iteration to the next. Expect a step-by-step execution trace rather than a list of syntax rules.
The emphasis on tracing the loop variable directly supports the first subtopic, loop structure, and builds the mental habit you need to spot a missing update that would cause an infinite loop.
Bro Code's fast-paced tutorial demonstrates the while statement in Python with worked examples, typically including input validation patterns where the loop keeps prompting until acceptable data is entered. It covers the basic syntax and the role of the condition that controls repetition.
It is a clear, concise demonstration of the classic use case for while loops — repeating until a condition is satisfied when the iteration count is unknown — from a widely used programming channel.
This short MaeMap video takes a for loop and rewrites it as an equivalent while loop, showing what must be added by hand: the counter initialization, the condition, and the increment. It makes the structural differences between the two loop forms explicit.
It targets the comparing for and while loops subtopic directly, and the side-by-side conversion makes clear why a forgotten increment leaves a while loop running forever.
These three videos build the while-loop half of MO2. Khan Academy establishes the structure and the trace-the-variable habit, Bro Code demonstrates practical Python syntax and the condition-driven use case, and MaeMap's for-to-while conversion clarifies when each loop form is the better choice. Because while conditions are built from the same comparison and logical operators used in if statements, the material also reinforces MO1, and the loops you practice here are the ones you will later use to iterate over lists and other collections in MO3.
These videos were chosen from YouTube on October 2, 2026. Each has captions; the player turns them on. Videos on YouTube can be changed or removed — if one no longer plays, tell your instructor. If a player does not appear, use its Watch on YouTube link.