Code patternsControlIf zero

If zero

Runs a piece of program once when a cell is zero.

Watch it run

The form “Flag next to x” works on 4 cells, named “x”, “flag”, “zero” and “out”. Press Play, drag the step slider forwards and back, or change x with the slider:

The figure needs JavaScript. The program below is the result for the example values.

A loop runs when a cell is not zero. For the opposite we need a second cell, a flag, that starts at 1.

If x is not zero, a loop on x knocks the flag down to 0. Then a loop on the flag runs the body: it runs exactly when x was zero.

In the main form the flag sits right next to x, and the two loops are arranged so that the pointer ends on the same cell both ways.

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 playground19 characters

What it needs

Common mistakes
  • Putting the flag anywhere else than directly right of x: the moves >-]> depend on it.
  • A body that leaves the pointer on another cell.

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
Flag next to x11 when x is not 0, else 166 when x is not 0, else 7
Flag anywhere, testing a copy22 + 25x9 when x is not 0, else 6
Using up x12 + 2x7 when x is not 0, else 4
Table 2. Measured when this page was written.
FormValuesBitsCharactersPlain stepsOptimised steps
Flag next to xx = 0819167
Flag next to xx = 5819116
Flag anywhere, testing a copyx = 0852226
Using up xx = 0819124

Other forms

Flag anywhere, testing a copy

Layout: x, out, flag, two scratch cells. Longer, but the cells may be anywhere.

>>+<<[->>>+>+<<<<]>>>>[-<<<<+>>>>]<[<->[-]]<[<+>-]<<
Open in the playground52 characters
Run this form step by step

The form “Flag anywhere, testing a copy” works on 5 cells; the named ones are “x”, “out”, “flag” and “scratch”. Press Play, drag the step slider forwards and back, or change x with the slider:

The figure needs JavaScript. The program below is the result for the example values.

Using up x

Layout: x, flag, out.

>+<[[-]>-<]>[>+<-]<
Open in the playground19 characters
Run this form step by step

The form “Using up x” works on 3 cells, named “x”, “flag” and “out”. Press Play, drag the step slider forwards and back, or change x 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.