Back
Close

Getting started with BrainFuck

DPAmar
4,410 views

If ..., then do ...

The very basic instruction in logic flow. The goal here is to execute a piece of code if and only if a value is true (i.e. not null).

And ideally, regardless of the boolean value, we want to end up at the same memory position. This is not mandatory, but it's really useful to know where we are in the memory, whatever might have been done earlier.

Let's start

  • Memory: A
  • Cursor: on A (condition to check)
  • Input: any

Process

  • while A is not null
    • do something
    • while A is not null
      • decrease A
    • loop
    • A=0
  • loop (but, wait, A=0, so the something is executed only once)

Code

[     while A is not null
  ... do something
  [-] while A is not null decrease A
]     loop (i.e. stop)

Minified version

[...[-]]

Final state

  • Memory: 0
  • Cursor: on 0
  • Input: unchanged
  • Output: unchanged
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants