Making a decision
By the end of this lesson you can make a program do something only if a cell is not zero, and something else if it is.
Only if there is something
Here is a small program with a box for its input, which starts as the letter a. Press Play, then empty the box and press Play again:
Notice that with the letter a the program printed an exclamation mark, and with an empty box it printed nothing. In both runs the part between the brackets ran once, or not at all.
The program reads a character into cell 0, and then [ asks its one question: is the cell under the pointer zero? With the letter a, cell 0 holds 97, so the machine goes in, makes 33 in cell 1 and prints it. With an empty box nothing is read, cell 0 stays 0, and the machine jumps past the closing ].
What makes the part run only once is the [−] just before the closing bracket. It empties cell 0, the cell the question is about. Without it the loop would print for ever. A loop that is made to run at most once is how brainfuck says if.
Is it a zero?
This program prints the letter N, unless the digit it reads is 0. Its first 48 commands are minus signs. Press Play with the input 5, then change the input to 0 and press Play again:
Notice that the 5 printed N and the 0 printed nothing. The character 0 is number 48 (Lesson 3 has the table), and 48 is not zero. So the program first takes 48 off. After that the cell is zero exactly when a 0 was typed, and the bracket can ask its only question.
Every comparison in brainfuck works like this. Is it equal to 48?
becomes is it zero after taking 48 off?
Or else
This program prints 1 if there is input. Write the missing part, so that it prints 0 if there is none. The words in it are comments, which the machine skips. Empty the input box to try the case without input:
Hint
The second pair of brackets runs only when the flag in cell 1 is still 1. Inside it you stand on cell 1.
Hint
Build 48 in a cell further right (6 rounds of 8), print it, empty it, and be back on cell 1 before the last two commands.
Notice that the finished program has two pairs of brackets, and that for any input exactly one of them runs. There is no command for otherwise
. The way round it is a second cell, called a flag: it is set to 1 before the question. If the first part runs, it clears the flag. Afterwards a second pair of brackets asks about the flag, and a flag that is still 1 means that the first part did not run.
Other languages write if ... else ...
. Brainfuck writes a flag and two loops that each run at most once.
Not for zero
Now use both ideas together. Write a program that reads one digit and prints an exclamation mark (number 33), but not if the digit is 0. It is tried with 5, with 0 and with 9:
Hint
Take 48 off first, as in Is it a zero?
Hint
Then the shape of Only if there is something: open a bracket, go to another cell, make 33, print, come back, empty the cell, close the bracket.
Notice that your program is two things you had already seen, one after the other: take 48 off, then an if
. You have joined a comparison to a decision, and that is enough for any choice a program has to make.
Check yourself
A free exercise
Read two characters and print an exclamation mark only if they differ. Take one off the other first. Harder: print = only if they are the same, which needs a flag. Nothing is checked here:
What you can do now
You can turn a comparison into the question is this cell zero?
, run a part of a program only if a cell is not zero, and use a flag for or else
. These are the pieces of this lesson:
| Piece | What it does |
|---|---|
[ … [−]] | Run the part in between once if the cell is not zero, and skip it if the cell is zero. The [−] at the end empties the cell, so the loop does not go round again. |
Take a number off, then [ | Ask whether a cell held that number: it is zero now only if it did. |
| A flag cell | Set a second cell to 1, clear it inside the first part, and ask about it with a second pair of brackets. That is or else. |
The Patterns have these shapes ready to use: if, if ... else and equal? The single question also has a price when the numbers get large; Lesson 4 of Build toward a chatbot shows it.