Understanding Boolean Values and Conditions
A while loop is a looping statement that repeatedly executes code as long as a condition is true.
Repetition Controlled by a Condition
A while loop repeats a block of code as long as its condition is true. Instead of writing the same instructions several times, you write the instructions once inside the loop body. Python evaluates the condition before each iteration and uses that Boolean result to decide whether the body should run.
The central cycle is condition, decision, loop body, and condition again. The cycle stops when the condition becomes false.
Tracing Each Loop Cycle
Every iteration follows the same order. Python evaluates the condition first. If the result is true, Python enters the loop body. After the body finishes, execution returns to the condition. The body does not run before the first condition check, and it does not run after a condition check produces false.
Tracing a Guessing Game
A guessing game keeps running until the user guesses the number 23. The guesses are 20, then 25, then 23.
First check: The running condition starts as true, so the loop body executes and the user makes the first guess, 20.
First result: The guess is not 23, so the game remains active. Execution returns to check the condition again.
Second check: The condition is still true, so the body executes again and the user guesses 25.
Second result: The guess is still not 23. The loop returns to its condition check.
Third check: The condition is true, so the body executes and the user guesses 23.
Termination change: The correct guess causes running to become false. After the body finishes, the next condition check is false, so the loop exits.
The loop executes once for each guess and stops after the correct guess changes the condition to false.
Boolean Decisions in Practice
A Boolean condition has two possible outcomes for the loop's decision: true or false. A true condition allows the loop body to execute. A false condition prevents another iteration and makes the loop exit immediately. This is why changing the condition variable inside the loop body is essential when the loop is meant to finish.
In this example, running controls repetition. While running is true, the body obtains and checks a guess. When the guess is 23, running changes to false. The next condition check therefore stops the loop. The optional else clause runs because the loop exits normally when the condition becomes false.
Knowing When Repetition Stops
A loop terminates normally when a condition check produces false. Usually, the loop body must change the variable or state used by the condition so that this can eventually happen. If nothing changes the condition, the condition can remain true and the loop can run forever. This is called an infinite loop.
Before writing a while loop, identify the condition variable and the statement that can change it. Then trace at least one complete cycle: condition check, body execution, condition-variable change, and next check.
Choosing While for Practical Problems
A while loop is useful when the number of repetitions is not known in advance. In the guessing game, the program cannot know how many guesses the user will make before reaching 23. The changing condition determines when the repetition ends.
The guessing game demonstrates a practical pattern: continue while the task is unfinished, perform one attempt in the body, update the condition when the task is complete, and let the next condition check end the loop.
| Loop choice | When it fits | Reason |
|---|---|---|
| while loop | The number of repetitions is not known in advance | A changing condition determines when execution stops |
| for loop | The number of repetitions or items is known | Iteration is handled automatically and is usually clearer and safer |
Mistakes Beginners Make
Forgetting to change the condition variable
The condition can remain true forever, producing an infinite loop.
Fix:
Make sure the loop body contains an action that can eventually make the condition false.Assuming the body runs before the first condition check
A while loop checks its condition before every iteration, including the first one.
Fix:
Trace the initial condition first. If it is false, the body is skipped.Confusing normal termination with break
The optional else clause runs only when the condition becomes false and the loop exits normally.
Fix:
Distinguish between a false condition and a break-based exit.
Practice the Execution Trace
Trace a while loop for a guessing game in which running starts as true, the target is 23, and the guesses are 20, 25, and 23. For each iteration, record whether the body runs, what the guess is, and when running becomes false.
Hints
- Begin with the condition check before the first guess.
- An incorrect guess leaves the game running.
- The correct guess changes running to false, but the loop exits at the following condition check.
Design a while-loop trace for a practical task where you do not know in advance how many attempts will be needed. Identify the Boolean condition, the action performed in the body, and the change that eventually makes the condition false.
Hints
- Use a condition that represents whether the task is still unfinished.
- Show the condition check before each attempt.
- Include the state change that allows normal termination.
Key Takeaways
- A while loop repeatedly executes its body while its condition is true.
- Python checks the condition before every iteration, so a false initial condition skips the body.
- The loop body should change the condition variable so the loop can eventually terminate.
- A while loop's optional else clause runs after normal termination, but not after break.
- Use a while loop when the required number of repetitions is unknown and depends on a changing condition.