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:

>++++++[->+++++++<]<[->>.<<]
Open in the playground28 characters

What it needs

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
Table 1. The cost as a function of the values. Exact formulas are checked on every case they run.
FormPlain stepsOptimised steps
Using up the counter76 + 7n3 + 3n
Keeping the counter79 + 13n5 + 4n
The body reads the countergrows with n × n1 + 4n
Table 2. Measured when this page was written.
FormValuesBitsCharactersPlain stepsOptimised steps
Using up the countern = 0828763
Using up the countern = 582811118
Using up the countern = 3082828693
Keeping the countern = 583714425
The body reads the countern = 482418317

Other forms

Keeping the counter

Layout: n, keep, star. Every round saves one unit in "keep".

>++++++[->+++++++<]<[->+>.<<]>[-<+>]<
Open in the playground37 characters
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.

[[->+>+<<]>>[-<<+>>]<<-]
Open in the playground24 characters
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

Sources

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.