More Loops
Speak only once
We already know one kind of loop. A FOR EACH loop runs its block once for every item in
a list.
But often there is no list to go through. We just want to do something a certain number of times, or keep going until something happens. CSP has two more loops for this.
REPEAT TIMES
A REPEAT TIMES loop runs its block a set number of times. We write the number between
REPEAT and TIMES.
This displays Hello! three times. Unlike FOR EACH, there is no loop variable, since
there is no list to take items from.
The number can be any expression, including a variable.
This displays 23 asterisks in a row.
Decimal Counts
The count does not have to be a whole number. If it is not, CSP rounds it up to the next whole number.
This runs 4 times, not 3. Any amount of a repetition left over counts as a full one.
Counting With a Variable
REPEAT TIMES does not tell us which iteration we are on. When we need that, we can
keep our own counter, using a variable created before the loop.
This displays 1 through 5, each on its own line.
REPEAT UNTIL
A REPEAT UNTIL loop does not count. Instead, it keeps running its block over and over
until a condition becomes true. The condition goes right after UNTIL, and it can be
anything that results in a boolean, just like the condition of an IF.
Before each iteration, CSP checks the condition. If it is false, the block runs again.
Once it is true, the loop ends and the program continues after the closing brace. Here
count goes up by one each time, so after the fifth iteration count = 5 is true and
the loop stops.
Because the condition is checked first, if it is already true when the loop begins, the
block never runs at all.
REPEAT UNTIL is a good fit when we do not know ahead of time how many iterations we
need. For example, we can keep asking the user for input until they give an answer we
accept.
Infinite Loops
It is very easy to create an infinite loop with
REPEAT UNTIL.
A loop is infinite if its condition never becomes true. The program then runs forever
and never reaches the code after the loop. The usual cause is forgetting to change
something that the condition depends on.
Nothing inside this loop ever changes count, so count = 5 stays false forever. The
fix is to add count ← count + 1 to the block.
There are subtler ways to go wrong, too. A condition that can be skipped over never
becomes true.
The value of count goes 2, 4, 6, 8, and so on. It jumps right past 5 and
never equals it. Using count ≥ 5 would stop the loop correctly.
Likewise, a loop whose condition is just false can never end.
When writing a REPEAT UNTIL, always ask yourself: what inside the loop will eventually
make this condition true? If you cannot answer, the loop is probably infinite.
FOR EACHandREPEAT TIMEScannot loop forever, because the number of iterations is decided before the loop starts. OnlyREPEAT UNTILcan, which is why it needs extra care.
Iteration Limit
Luckily, an infinite loop will not freeze your browser forever. CSP stops any loop that runs more than 100,000 iterations and reports an error.
This calculates 34 * 34 100,000 times, and then the program stops with an error
instead of running on indefinitely.
The limit is a safety net, not something to rely on. A program that hits it has a bug, and the output it produced before stopping is usually not what you wanted.
The limit applies to REPEAT TIMES as well. A REPEAT TIMES loop that requests to be run
over 100,000 times will crash before running.
Because of the limit, no CSP loop can truly run forever. Even so,
FOR EACHandREPEAT TIMESdecide their number of iterations before they start, so they only reach the limit if we ask for too many. OnlyREPEAT UNTILcan reach it by accident, which is why it needs extra care.
Choosing a Loop
| situation | loop |
|---|---|
| Do something with every item in a list | FOR EACH |
| Do something a known number of times | REPEAT TIMES |
| Keep going until something happens | REPEAT UNTIL |