Brainfuck Tutorial – part 3 (for expertes only!)

Ok, now let’s move into even more advanced stuff.

In this tutorial I will talk about conditions and booleans.

For making conditional statements, we need []. Let’s say you want to run a code only if a cell is ‘true’ (non zero). This one is the easiest!

[ [-] your code ]

“your code” only runs if the initial cell is non zero. After entering the first loop it clears the cell so it won’t repeat ‘your code’ again and again.

Let’s say you want to run a code if a cell is not true. Our structure is like this

..ab..
..^

And here is the code. I put some comments (numbers) into code so hopefully it will be easier to understand.

1>+< 2[[-]>-<] >[ - your code ]<

We need a temporary cell for this stuff. In this example a is the test value and 'your code' will only run if it is 0. b is the temporary cell.

First (1) it sets b to 1. After that (2) it returns to a and sets be to zero again if a is non zero. In next step (3) it goes back to b and run your code only if b is non zero.

Basically we are holding !a in another cell. If a is zero, b remains 1 and code will be executed. If a is non-zero, b will be cleared so 'your code' won't run.

After those simple booleans, on next tutorial, I will talk about == and !=. It is actually quite easy, see ya!