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:
[>+<[-]]
What it needs
- Destroying form: nothing. Copy form: two scratch cells. No-copy form: a zero cell on each side of x.
- The body must come back to the cell it started on.
- Any cell size.
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
| Form | Plain steps | Optimised steps |
|---|---|---|
| Using up x | 6 + 2x when x is not 0, else 1 | 4 when x is not 0, else 1 |
| Keeping x, by testing a copy | 14 + 21x when x is not 0, else 9 | 7 when x is not 0, else 4 |
| Keeping x, without a copy | 11 when x is not 0, else 4 | 6 when x is not 0, else 3 |
| Form | Values | Bits | Characters | Plain steps | Optimised steps |
|---|---|---|---|---|---|
| Using up x | x = 0 | 8 | 8 | 1 | 1 |
| Using up x | x = 5 | 8 | 8 | 16 | 4 |
| Keeping x, by testing a copy | x = 5 | 8 | 35 | 119 | 7 |
| Keeping x, without a copy | x = 5 | 8 | 11 | 11 | 6 |
Other forms
Keeping x, by testing a copy
Layout: x, out, two scratch cells.
[->>+>+<<<]>>>[-<<<+>>>]<[<+>[-]]<<
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.
[>>+<]<[<]>
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
- In the project:
ifNonZero(destroying) andwhenNonZero(copy) in emit.js. - Brainfuck basics, lesson 8, "Making a decision", teaches it. Build toward a chatbot, lesson 4, "Only one question", is about the only question brainfuck can ask.
- If and else builds on the no-copy idea.
Sources
- Source: Esolang wiki: Brainfuck algorithms: section "if (x) { code }": three forms; the second needs the repair described here.
- Source: This project: emit.js and patterns.js: ifNonZero, whenNonZero.
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.