esolang
Learn · LessonsLesson 3 of 18

Lesson 3

Loops in brainfuck

[ jumps past its matching ] when the cell under the pointer is zero; ] jumps back to its [ when it is not. That is the only control flow brainfuck has.

The lesson

In this implementation, reading past the end of the input gives 0. So ,[.,] copies the whole input. It prints, reads again and stops on 0.

Brainfuck has its own page, and Run opens this example in the editor.

Example

Output


Exercise 1

Print every input byte plus one (HAL becomes IBM).

Run tries your program on the input below. Check runs it on every test.

The tests they must pass
InputExpected output
HALIBM
helloifmmp
abc xyzbcd!yz{

Output


A hint

Start from the copy loop and change one byte before printing.

One answer

Answer: Print every input byte plus one (HAL becomes IBM).

Expected output

IBM
The tests they must pass
InputExpected output
HALIBM
helloifmmp
abc xyzbcd!yz{

Output


Exercise 2

Print every input byte twice (ab becomes aabb).

Run tries your program on the input below. Check runs it on every test.

The tests they must pass
InputExpected output
abaabb
hellohheelllloo
x yxx yy

Output


A hint

Printing does not change the cell, so print it again.

One answer

Answer: Print every input byte twice (ab becomes aabb).

Expected output

aabb
The tests they must pass
InputExpected output
abaabb
hellohheelllloo
x yxx yy

Output


Read more