Example 1 — Trace the loop by hand
Show the steps and the answer
First visit to the diamond n < 20?: 3 < 20 is true, so take the yes branch and double, n = 6.
In this flowchart the test comes before the body, so even the first doubling has to be earned by a true test.
Follow the arrow back to the diamond: 6 < 20 is true, n = 12. Back again: 12 < 20 is true, n = 24.
After every pass the arrow returns to the same test before anything else happens.
Next test: 24 < 20 is false, so take the no branch to OUTPUT n, which prints 24.
The only exit is the no branch, and control reaches it only after n has already gone past 20.
Tally: 3 passes (to 6, 12 and 24) and 4 tests, the last one false.
A while loop always ends on a failed test, so it makes one more test than it has passes.
Answer: The body runs 3 times (3 → 6 → 12 → 24) and the program prints 24.
Takeaway: Keep two tallies when tracing a while loop, passes and tests; the value printed is the first one to fail the test, which is why it is above 20.