Search code examples
loopsbrainfuck

Exit condition loop BrainF*ck


I've used the BrainFuck Visualizer to check out this small piece of code but I don't understand how the exit conditions work on the loops starting with -. It suddenly just stops and jumps to the next statement. Could anyone explain this to me?

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

Solution

  • I figured it out: Since you start at [0] and increment it by 1 ([1]), then start looping and subtracting 5 each time (going round to [255] again), you will hit [1], [2], [3], [4] as rest as you pass 4 times through the loop. When you finally land on the rest being [0], that's when the loop exits. In the meantime, the second spot on the tape has reached [103] which gets incremented by 1 to reach [104] to then be printed as the first letter h of hi