LearnBrainfuck basics

A small program

By the end of this lesson you can write a whole program from a description: plan the cells, build it in parts, and test it.

One gap

This program reads a digit and prints it, and it is almost what is asked for. Change it so that it prints the next digit: 3 gives 4. It is tried with 3, 0 and 8:

A plan and a gap

This program should print the digits 0123456789. Its plan is written into it as words, which the machine skips, and the cells are set up for you. Write the inside of the loop:

A plan in words

Now the plan is three lines above the box, and the program is yours. Write a program that reads one digit and prints that many stars: 3 gives ***. It is tried with 3, 0 and 9:

1. Read the digit into cell 0 and take 48 off, so that it is a number.
2. Put 42, the star, in cell 2.
3. Loop on cell 0: print cell 2, take one off cell 0.

Cells: 0 holds the number of stars still to print, 1 is free for building numbers, 2 holds the star.

On your own

This time there is only a sentence. Write a program that reads one digit and counts down from it to 0: 3 gives 3210. Plan the cells first. The hints go from a small push to the whole shape:

Check yourself

A free exercise

Change your countdown so that it ends with an exclamation mark. Or make it count up, from 0 to the digit. Nothing is checked here:

What you can do now

You can write a small program from a description: plan the cells, build it in parts, test each part, and try the edges.

The programs of A plan in words and On your own come back as the challenges That many stars and Count down from, where your program is also measured: how many commands, how many steps.

Programs are read as often as they are written. Lesson 10 is about reading brainfuck that somebody else wrote.