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:

>+<[>>>+<<-]>[>>>+<<<->]<<
Open in the playground26 characters

What it needs

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
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 x16 when x is not 0, else 187
Testing a copy17 + 15x when x is not 0, else 1810 when x is not 0, else 6
Using up x15 + 2x when x is not 0, else 148 when x is not 0, else 4
Table 2. Measured when this page was written.
FormValuesBitsCharactersPlain stepsOptimised steps
Flag next to xx = 0826187
Flag next to xx = 5826167
Testing a copyx = 58419210
Using up xx = 5824258

Other forms

Testing a copy

Layout: x, flag, scratch, then, else. Works for any layout, costs a copy.

[->+>+<<]>[-<+>]+>[>+<<->[-]]<[>>>+<<<-]<
Open in the playground41 characters
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.

>+<[[-]>->+<<]>[>>+<<-]<
Open in the playground24 characters
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

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.