Understanding for Loops
The central idea is repetition controlled by a condition: check running, execute the loop block, check running again, and either repeat or continue after the loop.
The Loop Behind the Guessing Game
The source example is presented under the topic of for loops, but the mechanism it demonstrates is a while loop. The program repeatedly asks for an integer, compares the guess with a target number, and gives feedback. It keeps repeating while the variable running is True. When the guess equals the target, the program prints a congratulatory message and changes running to False.
The central idea is repetition controlled by a condition: check running, execute the loop block, check running again, and either repeat or continue after the loop.
One Complete Loop Cycle
Before the loop block runs, the program checks whether running is True. If it is, the block executes. The block obtains a guess and uses conditional statements to compare that guess with number. After the block finishes, the loop condition is checked again. If running is still True, another iteration begins. If running has become False, the loop ends and its optional else block can run.
Following the Guess Sequence
Three Attempts
Trace the source example when the entered guesses are 50, 22, and 23.
- 1First attempt: 50
The guess is higher than the target. The program prints, "No, it is a little lower than that." The loop continues.
- 2Second attempt: 22
The guess is lower than the target. The program prints, "No, it is a little higher than that." The loop continues.
- 3Third attempt: 23
The guess equals the target. The program prints, "Congratulations, you guessed it." The running variable is changed to False.
- 4After the loop
Because the loop condition is now False, the while loop ends. Its else block prints, "The while loop is over," and the next statement prints, "Done."
The output order is: the lower message, the higher message, the congratulations message, the while-loop-over message, and Done.
Enter an integer : 50
No, it is a little lower than that.
Enter an integer : 22
No, it is a little higher than that.
Enter an integer : 23
Congratulations, you guessed it.
The while loop is over.
DoneChoosing the Feedback Branch
The first test asks whether guess == number. If that comparison is true, the program prints the congratulations message and makes the loop stop. If the first test is false, the elif test asks whether guess < number. A true result produces the message that the target is a little higher. If neither comparison is true, the final else produces the message that the target is a little lower.
| Comparison result | Selected branch | Message or action |
|---|---|---|
| guess == number | if | Print congratulations and make running False |
| guess < number | elif | Print that the target is a little higher |
| Neither comparison is true | else | Print that the target is a little lower |
The conditional branches used inside the loop
What Happens After Repetition
The while loop has an optional else block. In this example, that else block prints "The while loop is over." It runs when the while-loop condition becomes False. The source also explains that the else block is always executed unless the loop is exited with a break statement. In the demonstrated path, the condition becomes False because the successful guess changes running to False, so the else block runs before the next statement prints "Done."
Mistakes in Loop Tracing
Common mistake Assuming the loop ends immediately after a comparison.
What it looks likeThe program compares a guess with number and then treats the loop as finished.
Why it failsA comparison selects a branch, but repetition is controlled by the running condition.
Do this insteadAfter each loop block, check the value of running again. The loop repeats while it is True.
Common mistake Forgetting that the successful branch changes the loop state.
What it looks likeThe guess equals number, but running remains True.
Why it failsIf running stays True, the loop condition remains true and another iteration can begin.
Do this insteadOn the successful comparison, track the assignment that changes running to False.
Common mistake Reversing the higher and lower feedback.
What it looks likeA guess of 22 is treated as higher than the target in the source sequence.
Why it failsThe source sequence uses 22 as a lower guess and therefore prints that the target is a little higher.
Do this insteadFor guess < number, select the message saying the target is a little higher. Otherwise, after the equality test fails, select the message saying the target is a little lower.
Common mistake Expecting the while-loop else block after every exit.
What it looks likeTreating a break exit as if it must print the while-loop-over message.
Why it failsThe source states that the else block is skipped when break exits the loop.
Do this insteadDistinguish normal termination through a False condition from termination caused by break.
A Reliable Tracing Method
- 1Write down the current value of running before the loop iteration begins.
- 2Record the current guess and compare it with number.
- 3Identify which of if, elif, or else supplies the message.
- 4Check whether the successful branch changes running to False.
- 5If running is still True, trace the next guess; otherwise, trace the while-loop else block and then the following statement.
Using the same control flow, explain what happens when the first guess is 22 and the second guess is 23. Identify the message from each attempt, the value of running after the successful guess, and whether the while-loop else block runs.
Hints
- Compare 22 with number and select the elif or else branch according to the source sequence.
- The successful guess changes running to False.
- A False loop condition causes the while-loop else block to execute.
Key Takeaways
- 1The source example uses a while loop controlled by the variable running.
- 2The loop checks its condition before each iteration and checks it again after the loop block finishes.
- 3The if, elif, and else branches choose feedback based on the relationship between guess and number.
- 4A correct guess changes running to False, which ends repetition.
- 5The while-loop else block runs when the condition becomes False, but not when break exits the loop.
At a glance
- A while loop repeats its block while its condition remains True.
- The guessing example compares each guess with a target number and selects one of three feedback branches.
- The successful branch changes running to False, allowing the loop to finish.
- The loop's else block runs after normal termination through a False condition, but not after break.