Code patternsControlIf and else
If and else
Runs one piece of program when a cell is not zero and another piece when it is zero.
Watch it run
The form “Flag next to x” works on 5 cells, named “x”, “flag”, “zero”, “then” and “else”. 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 flag next to x starts at 1. If x is not zero, the first loop runs the "then" part, clears the flag and stops on the flag cell.
One step to the right is then a zero cell, so the second loop is skipped. If x was zero the first loop is skipped, that same step lands on the flag, which is still 1, and the "else" part runs.
Both ways end on the same cell. That is the whole trick: after a decision the program must know where the pointer is.
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: flag and one more zero cell directly right of x. Both are zero at the end; x is kept.
- Each part starts and ends on a fixed cell (here: the "then" part on x, the "else" part on the flag).
- Any cell size.
Common mistakes
- Changing x inside the "then" part. The first loop leaves through the flag cell, not through x, so that is allowed, but the layout must stay x, flag, zero.
- Running two separate ifs, "if x" and "if not x", when the first one changes x.
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 | 16 when x is not 0, else 18 | 7 |
| Testing a copy | 17 + 15x when x is not 0, else 18 | 10 when x is not 0, else 6 |
| Using up x | 15 + 2x when x is not 0, else 14 | 8 when x is not 0, else 4 |
| Form | Values | Bits | Characters | Plain steps | Optimised steps |
|---|---|---|---|---|---|
| Flag next to x | x = 0 | 8 | 26 | 18 | 7 |
| Flag next to x | x = 5 | 8 | 26 | 16 | 7 |
| Testing a copy | x = 5 | 8 | 41 | 92 | 10 |
| Using up x | x = 5 | 8 | 24 | 25 | 8 |
Other forms
Testing a copy
Layout: x, flag, scratch, then, else. Works for any layout, costs a copy.
[->+>+<<]>[-<+>]+>[>+<<->[-]]<[>>>+<<<-]<
Run this form step by step
The form “Testing a copy” works on 5 cells, named “x”, “flag”, “scratch”, “then” and “else”. 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, then, else.
>+<[[-]>->+<<]>[>>+<<-]<
Run this form step by step
The form “Using up x” works on 4 cells, named “x”, “flag”, “then” and “else”. 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
- Less than and saturating subtract use it once per round.
- The table lookup of the project is a tree of these, one per bit.
Sources
- Source: Esolang wiki: Brainfuck algorithms: section "if (x) { code1 } else { code2 }": the forms by Ben-Arba (main) and Jeffry Johnston (copy).
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.