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:
>+<[>-]>[>>+<<->]<<
What it needs
- Main form: the two cells to the right of x must be zero. They are zero again at the end, and x is kept.
- The body starts and ends on the flag cell.
- Any cell size.
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
| Form | Plain steps | Optimised steps |
|---|---|---|
| Flag next to x | 11 when x is not 0, else 16 | 6 when x is not 0, else 7 |
| Flag anywhere, testing a copy | 22 + 25x | 9 when x is not 0, else 6 |
| Using up x | 12 + 2x | 7 when x is not 0, else 4 |
| Form | Values | Bits | Characters | Plain steps | Optimised steps |
|---|---|---|---|---|---|
| Flag next to x | x = 0 | 8 | 19 | 16 | 7 |
| Flag next to x | x = 5 | 8 | 19 | 11 | 6 |
| Flag anywhere, testing a copy | x = 0 | 8 | 52 | 22 | 6 |
| Using up x | x = 0 | 8 | 19 | 12 | 4 |
Other forms
Flag anywhere, testing a copy
Layout: x, out, flag, two scratch cells. Longer, but the cells may be anywhere.
>>+<<[->>>+>+<<<<]>>>>[-<<<<+>>>>]<[<->[-]]<[<+>-]<<
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.
>+<[[-]>-<]>[>+<-]<
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
- In the project:
whenZeroin emit.js (the flag form). - The "not" of not, and, or on flags is this pattern with "set a flag" as its body.
- Build toward a chatbot, lesson 4, "Only one question", measures what counting down costs.
Sources
- Source: Esolang wiki: Brainfuck algorithms: section "if (x) { code1 } else { code2 }", the form by Ben-Arba with an empty first part.
- Source: This project: emit.js and patterns.js: whenZero.
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.