Code patternsControlIf non-zero

If non-zero

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

Watch it run

The form “Using up x” works on 2 cells, named “x” 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 is the only thing in brainfuck that looks at a cell. So an "if" is a loop that is made to run at most once.

The simplest way to stop it after one round is to clear the cell at the end of the body. That destroys the value.

To keep the value, either test a copy (see copy a value), or leave the loop by stepping onto a cell that is known to be zero and then find the way back.

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

What it needs

Common mistakes
  • Leaving the loop on another cell than the one you entered on, and then moving as if you knew where you are. See the note "Wrong as often given" under this list.
  • A body that changes the tested cell and forgets that the closing bracket tests it again.

Wrong as often given

A short form that is often given is "x[ code temp ]x": leave the loop by walking to an empty cell, then walk back to x. Written with fixed moves this is wrong when x is zero: the loop is skipped, the pointer is still on x, and the walk back goes one cell too far. The form Keeping x, without a copy on this page repairs it with [<], which stops on the zero cell left of x in both cases.

Each wrong form is run, and it fails in exactly this way.

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 x6 + 2x when x is not 0, else 14 when x is not 0, else 1
Keeping x, by testing a copy14 + 21x when x is not 0, else 97 when x is not 0, else 4
Keeping x, without a copy11 when x is not 0, else 46 when x is not 0, else 3
Table 2. Measured when this page was written.
FormValuesBitsCharactersPlain stepsOptimised steps
Using up xx = 08811
Using up xx = 588164
Keeping x, by testing a copyx = 58351197
Keeping x, without a copyx = 5811116

Other forms

Keeping x, by testing a copy

Layout: x, out, two scratch cells.

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

The form “Keeping x, by testing a copy” works on 4 cells; the named ones are “x”, “out” 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.

Keeping x, without a copy

Layout: zero, x, zero, out. The pointer starts on x. Leaving the loop on the zero cell costs nothing; <[<]> finds x again.

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

The form “Keeping x, without a copy” works on 4 cells; the named ones are “zero”, “x” 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.