Loops
By the end of this lesson you can make the machine repeat a piece of program, and empty a cell whatever it holds.
Round and round
This program puts 3 in cell 0, and then comes a pair of square brackets around a minus. Press Step until it has ended, and read the question the line under the tape asks at each bracket:
Notice that the cell counted down, 3, 2, 1, 0, and that each time the machine reached a bracket it looked at the cell and asked whether it was zero. At [, zero means: jump past the matching ]. At ], not zero means: back to the [.
The square brackets mean: repeat what stands between us until the cell under the pointer is zero. Such a repeat is called a loop, and one pass through it is a round. This loop holds one command, −, so it counts the cell down.
Empty a cell
Cell 0 holds 200. Empty it with a program of at most 3 commands:
Hint
The loop of step 1 emptied a cell that held 3. Did it matter that it was 3?
[−] empties a cell whatever it holds: take one off, until it is zero. You will type these three symbols more often than any others.
Notice that minus signs alone could not have done it within the limit, and that a program often does not know the number. A loop adapts: it goes round as often as needed.
A loop that moves something
Now a loop with more inside it: it takes 1 from cell 0 and adds 3 to cell 1. Press Play, or step through it, and watch both cells until the program has ended:
Notice that every round took 1 from cell 0 and gave 3 to cell 1. Cell 0 counted the rounds: there were four, so cell 1 holds 12.
Each round ended where it began, on cell 0. That matters: the bracket asks about the cell under the pointer, so the pointer has to be back on the counter when the ] comes.
A loop that never ends
Here the loop has nothing inside it. Drag the slider all the way to the right, and read the line under the tape. The figure stops after 500 steps, so that nothing freezes:
Notice that the cell stayed at 1 and the machine kept asking the same question. +[] makes the cell 1 and then asks, again and again, whether it is zero. Nothing inside the loop changes the cell, so the answer never changes.
This is an endless loop, the second classic mistake after the pointer that is not where you think it is, from Lesson 2. A loop ends only if something inside it brings its cell to zero.
A loop that never starts
This time the cell holds 0 when the loop begins, and one more command follows the loop. Press Step until the end and watch what the first bracket does:
Notice that the three plus signs never ran: the cell was already zero at the [, so the machine jumped straight past the ]. Only the last +, after the loop, ran.
So a pair of brackets is also a question: do this only if the cell is not zero. Lesson 8 is built on that.
Check yourself
A free exercise
Write a loop that goes round 5 times and adds 2 to cell 1 each round, so that cell 1 ends at 10. Nothing is checked here:
What you can do now
You can repeat with [ and ] until a cell is zero, empty a cell with [−], and spot a loop that cannot end. These are the commands you know:
| Command | What it does |
|---|---|
+ − | Add 1 to the cell under the pointer, or take 1 off. |
> < | Move the pointer one cell to the right or to the left. |
. | Print the cell as one character. |
[ ] | Repeat everything between them until the cell under the pointer is zero. |
Brackets must match: every [ needs its own ] further on. Loops may stand inside loops. A program whose brackets do not match is the only kind of brainfuck program that is refused before it runs.