2026.06.14 22:06
how to make an if statement in brainfuck the way I like it

I was coding in brainfuck recently and stumbled upon a problem: how to make a normal if statement? It seems simple enough – use the [ and ] commands to make a loop that runs at most once. But since this is supposed to be a normal if statement inside a program, in practice I'd usually want the memory pointer (you know, the one moved with < and >) to end up in the exact same spot regardless of whether we entered the if block or skipped it. So, here are my thoughts on how to do it.

First of all, if I just write an if statement the first way that comes to mind, it won't do what I want. Look at this negation, for example - a piece of code that:

>+<[>-]

You can test this yourself on a Brainfuck visualizer website. If you run this code as shown, you'll see what happens when the first cell is 0. If you want to see what happens when the first cell is 1, just add a + at the very beginning of the program.

And as you can see, the negation works, but depending on whether we enter the if block or not (meaning, whether the [ command jumps a few commands to the right or not), we end up either in the first cell or the second cell after the if statement.

When T. was fighting this problem, he came up with a workaround: okay, we will land in different memory cells after the if, but then let's have a special piece of code that resets the memory pointer to a fixed position. For example, let's have a sequence of ones preceded by a zero somewhere to the side, move the memory pointer into the middle of it (since there are many ones, we don't have to aim precisely), and then use a loop going left until it hits zero. Something like this, for example:

>>>>+>+>+>+<<<<<<<
>+<[>-]
>>>>>
[<]

Cool, it works. But I don't like that it's so wasteful – I have to keep that area with ones in memory (sure, it could be shorter, but still), and I have this extra block in the code after the if statement.

So I kept brainstorming how to do this. Look: if I want the memory pointer to be in the same place after the ] command regardless of whether [ skipped the if body or not, then at the ] command, the memory pointer must be on the exact same cell it was on during the [ command. Because if the [ command jumps, the ] command comes right after it, so the memory pointer will be the same. On the other hand, at the ] command, if we want to exit the if body (meaning we don't want to do another loop iteration), the memory pointer must point to a cell that holds a zero (because that's how the ] command works).

But at the [ command, the memory pointer must point to a cell that is sometimes 0 and sometimes 1 – because that's the whole point of an if statement. Conclusion: for the if to work the way I want (to finish with the memory pointer on the same cell regardless of whether the if condition was met or not, meaning whether we entered the if body or not), the if statement must destroy its own condition cell. Meaning, after the if executes, the condition cell must be zeroed out. There's no other way. Simply put, at the end of the body, the if must move the memory pointer back to where it was at the [ command and zero out that memory cell so that the ] command doesn't loop.

Look, here is how you can do it – this is a negation similar to the previous one, but it always ends up with the memory pointer on the first cell (the negated one), though it zeroes it out in the process:

>+<
[>-<-]

But whether this is better than T.'s method, I don't know, it depends. The code is a bit shorter and it doesn't use extra memory, but it destroys the negated cell. So if I want to keep it, I have to copy it to the side first and run the negation there, which isn't free either.



comments:

nickname:

enter digit "four": (this is a spam protection)

offensive comments or those I don't like will be deleted


back to homepage

RSS