Code patternsControlA counted loop
A counted loop
Runs a piece of program a given number of times.
Watch it run
The form “Using up the counter” works on 3 cells, named “n”, “scratch” and “star”. Press Play, drag the step slider forwards and back, or change n with the slider:
The figure needs JavaScript. The program below is the result for the example values.
The counter is the loop cell. Each round takes 1 off; when it reaches zero the loop ends.
The counter is used up. To keep it, each round also adds 1 to a scratch cell, and one move at the end puts it back (see move a value).
The body may read the counter too, as long as it leaves it where it was: the form The body reads the counter adds the counter itself to a total each round.
The code
This is the program the figure ran. It is brainfuck as it stands: copy it, or open it in the playground, where you can change it:
>++++++[->+++++++<]<[->>.<<]
What it needs
- The body must return to the counter cell.
- Any cell size. A count of zero runs the body zero times.
Common mistakes
- Taking 1 off at the start of the round in one place and at the end in another. Both work; mixing them up inside one body does not.
- A body that is not "flat" (it prints, or holds a loop): the optimiser cannot fold the loop, so the cost is per round. See the cost page.
What it costs
A plain step is one command. An optimised step is one step of an interpreter that folds runs of the same command and flat loops. Choose the values and watch the two bars; What costs what explains how cost is counted:
The figure needs JavaScript. The table below has the measured steps.
Cost as a formula, and measured
| Form | Plain steps | Optimised steps |
|---|---|---|
| Using up the counter | 76 + 7n | 3 + 3n |
| Keeping the counter | 79 + 13n | 5 + 4n |
| The body reads the counter | grows with n × n | 1 + 4n |
| Form | Values | Bits | Characters | Plain steps | Optimised steps |
|---|---|---|---|---|---|
| Using up the counter | n = 0 | 8 | 28 | 76 | 3 |
| Using up the counter | n = 5 | 8 | 28 | 111 | 18 |
| Using up the counter | n = 30 | 8 | 28 | 286 | 93 |
| Keeping the counter | n = 5 | 8 | 37 | 144 | 25 |
| The body reads the counter | n = 4 | 8 | 24 | 183 | 17 |
Other forms
Keeping the counter
Layout: n, keep, star. Every round saves one unit in "keep".
>++++++[->+++++++<]<[->+>.<<]>[-<+>]<
Run this form step by step
The form “Keeping the counter” works on 3 cells, named “n”, “keep” and “star”. Press Play, drag the step slider forwards and back, or change n with the slider:
The figure needs JavaScript. The program below is the result for the example values.
The body reads the counter
Adds n + (n − 1) + … + 1. Layout: n, total, scratch.
[[->+>+<<]>>[-<<+>>]<<-]
Run this form step by step
The form “The body reads the counter” works on 3 cells, named “n”, “total” and “scratch”. Press Play, drag the step slider forwards and back, or change n with the slider:
The figure needs JavaScript. The program below is the result for the example values.
Where it is used
- Multiply two cells is a counted loop round a copy.
- Print a constant string: the loop that seeds the cells.
Sources
- Source: Esolang wiki: Brainfuck algorithms: section "Summing 1~n" for the idea of the third form; the code here is our own.
Every program on this page was run on many inputs, in plain and in optimised mode, and compared with JavaScript. The explanations are written for this page.