Learn to Code via Tutorials on Repl.it!

← Back to all posts
The Ultimate BrainF Tutorial!
Th3Coder (68)

Introduction

Think about any programming language that you know. Anything at all. Java? Python? JavaScript? C? C#? C++? Kotlin?

Well, you're going to learn something extremely different from all that!

BrainF (I think you can guess what the F stands for right?) is one of, if not the hardest programming language ever!

Despite that, however, you can create nearly anything using a BrainF program!

Btw, stay tuned for a challenge at the end!

And mark my words, this will be ridiculously hard!

Don't believe me?


A Simple Start: The Hello World Program!

Of course, Hello World!

In Python, we use:

print("Hello World!")

In JavaScript:

console.log("Hello World!");

In C:

#include <stdio.h>
int main(){
  printf("Hello World!");
  return 0;
}

So, this can't be hard, right?
RIGHT?

Now, let me introduce you to THE BRAINF HELLO WORLD PROGRAM

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

Now, before you scream, BrainF only uses 8 (yes, 8) characters!

And even before that, we need to understand what exactly is going on.


Simple Operators

Imagine being in a hotel. Now, the logic goes like this:

  • You're inside room 1 in the hotel. There, you can put people inside the room! As many as you want. You need to use the keyword + to add people to the room!

For instance:

+++++

There, you put 5 people inside room number 1.

  • Then, you can decrease the amount of people in a room! You use the - keyword!

For instance:

+++++--

That was useless, but now there are 3 people in the room!

It's the same as coding:

+++

Moving Pointers

  • Now, you can move from room 1 to room 2,then to room 3, room 4 etc. There are unlimited rooms because this is Hilbert's Hotel!

How do you move?

  • You move to the next room by using the right pointer >, and move to the previous room by using the left pointer <!

  • Let's say you want to add 3 people to room 1, then 2 people to room 2!

You need to code:

+++>++

Let's break the code down.

+++ adds 3 people to the room you are currently on, which is room 1!

> moves you to next room, which is room 2!

++ adds 2 people to the room you are currently in, which is room 2!


Comments

Comments are basically hints you can write to make yourself or others understand the code! Think about it as writing on the hotel's walls, it certainly won't do anything to the people!

But it might make the manager angry though...

BrainF automatically ignores every single character that is not one of the 8 characters!

So, you can safely code:

+++ This adds 3 people to room 1
> Moves pointer to room 2
++ Adds 2 people to room 2

And no, Syntax errors are not a thing in BrainF. Trust me.

Yes, yes I know we haven't printed anything yet. That's what we will do next!


Printing and taking user input

  • Like what we said before, printing in BrainF is REALLY frustrating. Get ready to read a lot, it's necessary to understand!

  • So how do you print in BrainF? Well, BrainF uses the Unicode System!

  • The Unicode System is basically a list of all the characters, symbols, glyphs, etc from EVERY language! You can see the entire list here!

  • Every Unicode character has a unique ID. Think of the ID as the amount of people in the room!

  • For instance, if there are 51 people in a room (yes, every room can fit infinite people) and you want to print it:

    • Look at the Unicode character table. Which character has the ID of 51?
    • It's 3! That means the output will be 3!
    • And don't forget: to print a room (or cell), use the keyword . (period)!
    • It will print the cell you're currently on!
  • And also, the keyword , (comma) will ask for user input!

    • I really recommend you to go to an empty cell before asking for user input, since the character that the user gives will be stored in its Unicode form!
    • For example, if the user inputs 3, then the value stored in the cell will be 51!

For instance:

Output 'Hi' and ask for input
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++. adds 72 to cell 1 and prints it

+++++++++++++++++++++++++++++++++. adds 33 to cell 1 (so it becomes 105) and prints it

> Goes to empty cell
, asks for user input

Oh wow, you need to type the keyword + 72 TIMES TO PRINT H???

No, not really. You can shorten it, that's what we'll learn next!


Loops

  • So far, we have learnt 6 characters:
    • + for adding values to a cell
    • - for decreasing values in a cell
    • > for moving pointer to the next cell (right)
    • < for moving pointer to the previous cell (left)
    • . for printing the character with the Unicode ID in a cell
    • , to ask for user input

Thus, what are the other 2 symbols?

  • They are [ and ](square brackets)!
  • They are used to make loops!

  • Think about it like this:

    • If the value of the current cell is not 0, then execute the code between them!
    • If the value of the current cell is 0,then skip the brackets and go to the next code!

For example:

+++[-]+

Let's break down the code!

+++ adds 3 to the current cell, which is cell 1!

[-] decreases the cell value by 1 if the value is not 0!

+ adds 1 to the cell's value!

The logic is:

  • Cell 1 has a value of 3! Keep that in mind!

  • Now, since the value of cell 1 is not 0, the value will be decreased by 1, which makes it 2!

  • The value of cell 1 is still not 0, so it will be decreased again to 1!

  • Still, the value is not 0 so it will be decreased by 1 and makes the value 0!

  • Since the value is 0, it will stop the loop and execute the next code!

  • So, it will increase the value to 1!

Now, let's look at another example!

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

Let's break the code down!

++++ adds 4 to cell 1!
[>++++<-] let's break it down even more!

  • > moves the pointer to cell 2
  • ++++ adds 4 to cell 2!
  • < moves the pointer back to cell 1
  • - decreases the value of cell 1 by 1!
  • All this happens while cell 1 is not 0!
    > moves the pointer to cell 2!

So, can you guess the value of cell 2 now? 🤔🤔🤔

If you answered 16, CONGRATULATIONS! YOU'RE RIGHT!


Ending (I guess)

Hey, guess what?

You just learnt the basics of one of the hardest programming languages in the world!

Now, take a moment to rest and admire what you have done.

Also, if you don't understand anything, feel free to go back and read it again!

And for the promised challenge...

  • Ask the user to input a number

  • If the number is exactly 1, then output 'one'.

  • Otherwise, output 'not one'

This is very easy and simple in other languages...

But in BrainF???

Hint: use loops, that's all I can tell you!

Link your code below!

Well, that's it for now. Thanks for reading and happy programming! :)

Comments
hotnewtop
TomAnacondo (6)

Hey, Th3Coder! Thanks for the vote!

Th3Coder (68)

@TomAnacondo hey, don't mention it! Your code is great, so you deserve it :)