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.

csp
You may also type <- !=>= <= for auto complete.

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.

csp
You may also type <- !=>= <= for auto complete.

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.

csp
You may also type <- !=>= <= for auto complete.

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.

csp
You may also type <- !=>= <= for auto complete.

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.

csp
You may also type <- !=>= <= for auto complete.

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.

csp
You may also type <- !=>= <= for auto complete.

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.

csp
You may also type <- !=>= <= for auto complete.

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.

csp
You may also type <- !=>= <= for auto complete.

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.

*csp
You may also type <- !=>= <= for auto complete.

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.

csp
You may also type <- !=>= <= for auto complete.

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.

*csp
You may also type <- !=>= <= for auto complete.

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 EACH and REPEAT TIMES cannot loop forever, because the number of iterations is decided before the loop starts. Only REPEAT UNTIL can, 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.

csp
You may also type <- !=>= <= for auto complete.

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.

csp
You may also type <- !=>= <= for auto complete.

Because of the limit, no CSP loop can truly run forever. Even so, FOR EACH and REPEAT TIMES decide their number of iterations before they start, so they only reach the limit if we ask for too many. Only REPEAT UNTIL can reach it by accident, which is why it needs extra care.

Choosing a Loop

situationloop
Do something with every item in a listFOR EACH
Do something a known number of timesREPEAT TIMES
Keep going until something happensREPEAT UNTIL

Next: Getting Started with Rendering