Flow control gives two ways to change a loop's normal run. One command ends the whole loop as soon as its condition is met, and program flow continues at the line after the loop. The other command skips the rest of the current pass and moves on to the next pass, and the loop keeps running.
Flow control commands only make sense inside a loop body, and each one affects only the innermost loop that contains it. In nested loops, the outer counter is increased once after the inner loop finishes, and the inner counter starts again on every outer pass.
Inside your account
What an account adds to the pages you are reading.
Lessons
Topic explanations with worked examples, split into units.
The command that ends a loop stops the whole loop at the moment its condition is met.
The command that skips a pass does not stop the loop, so the loop continues with the next pass.
If the increment step is placed at the end of the body, a skip command can jump past it and cause an infinite loop.
In nested loops, the inner counter restarts each time the outer loop runs.
If the condition is false at the start, the loop body never runs; if the condition never becomes false, the loop runs forever.
Example
Question: from a list of twenty records, we want to copy only the records that are greater than zero into a new list, and the list contains seven negative records. How many records are copied? If the list has no zero values, the remaining 13 records are copied to the new list, and the count is printed.
Question: a list of four records contains 105, 112, 120 and 137. The target is 137. At which record does the loop close? The loop closes at the fourth record, because the target is found there and the loop ends.
Common mistakes
Confusing the two commands. The one that ends a loop stops the whole loop, while the one that skips a pass keeps the loop running.
Skipping the counter increment. If the skip command returns to the start of the loop before the increment at the end, the counter never changes and the loop never ends.
Writing a condition that never changes. A constant condition returns the same result every time, so the loop runs forever.
Summary
Loop flow control has two commands: one ends the loop and the other skips the current pass. Their effects are opposite, so placing each one correctly matters. Increment steps must be placed so that skipping does not stop the counter, and nested loops keep their counters separate.