BrainFu / Tutorial / Page 3
Learn Programming in 30 minutes, Here and Now
Page 3 of 3
Published 2024/04/30
This chapter first explains how to create iterations in the BrainFu language. The tutorial is then concluded, with suggestions for further reading.
The First Loop
Let's examine one simple program which uses symbols [and ] to create a loop. You should copy this program into the interpreter, and run it. This program outputs the text “##$$$”.
++++ +++ [ >+++ ++ <- ] > .. +...
The provided program first generates a value 7 in the first cell. It then uses a loop to increase the second cell by 5, repeated 7 times. Therefore, the value 5 × 7 = 35 will be generated in the second cell.
After the loop, the program outputs the generated value two times by the output commands .. . To figure out which symbol will be output, we have to find the symbol with the code 35 in the ASCII code table. The ASCII symbol with the code 35 is “#”, because 35 equals 32 + 3, and the symbol at the intersection of 32 and 3 is “#”. Two symbols “#” will be output.
The program then increases the value of the cell by 1. The ASCII symbol with the code 36 is “$”, at the intersection of 32 and 4. The program then outputs that symbol three times.
It is very important that you to carefully examine this program with the debugger, by stepping through the instructions one by one. Keep the “
” interface open to monitor changes of cell values.Outputting “11%” with a Loop
The next program outputs “11%”. You should copy this program into the interpreter, and run it.
++++ +++ [ >+++ ++ >++++ +++ <<- ] >> .. < ++ .
The program generates two values with a single loop. The values 49 is the ASCII code for the symbol “1”, which gets output two times.
The program then increases the value 35 by 2 to get the value 37. The values 37 is the ASCII code for the symbol “%”, which is output once.
You should carefully examine the program with the debugger. Step through the instructions slowly to get a better understanding of the program.
Outputting New Lines and the %%dbg
Directive
The next program outputs the text “11%”, but with each character in its own line. You should copy this program into the interpreter, and run it.
++++ +++ [ >+++ ++ >++++ +++ >+ <<<- ]
%%dbg > ++ >> +++
%%dbg2 <.>.
<.>.
<<.>>.
The directive %%dbg
can be easily spotted in
the source code. This directive has an effect only when the debugger is enabled.
Enable the debugger, and then click on the
%%dbg
directive
is to stop the program when the directive is executed.
Click on the
button, and the program will stop executing
just after the first %%dbg
directive.
At that point, the program's main loop has completed and it has generated three values in the memory cells: 35, 49 and 7. The second line of the program will then modify those values to 37, 49 and 10.
The third line of the program first outputs the value 49, followed by the value 10. The value 49 gets output as a symbol “1”.
The value 10 is a special symbol, called the “Line Feed” symbol. Outputting the value 10 will cause the output to be continued in the next output line.
The fourth line of the program first outputs the value 49, followed by the value 10. Thus, a symbol “1” gets output in its own line.
The last line of the program first outputs the value 37, followed by the value 10. Thus, a symbol “%” gets output in its own line.
You should use the debugger to carefully examine this program.
Outputting Text “Hello World!”
The next program outputs the text “Hello World!”. You should first copy this program into the interpreter, and run it.
++++ ++++ +++ [ >+++ +++ >+++ +++ +++ >+++ >++++ ++++ >+ <<<<<- ]
Output “H” : >+++ +++.
Output “e” : >++.
Output “ll”: +++ +++ +..
Output “o” : +++.
Output “ ” : >-.
“W”: >-. “o”: <<. “r” +++. “l”: ------. “d”: --------. “!”: >+.
Output a New Line: >>-.
The first line of the program generates the values 66, 99, 33, 88, 11. The other lines output the letters, as indicated by the comments.
You should use the debugger to examine this program in more detail.
Where to Go Next?
This tutorial has explained the basics of computer programming in the BrainFu programming language.
After you have completed this tutorial, you should have acquired a much better understanding of inner workings of computers.
If you would like to learn more about the BrainFu language, we recommend the website Esolang - BrainFu**, which has plenty of resources about this very interesting language. However, you should be warned that the language BrainFu is one of the most difficult programming languages to use for writing programs. The language BrainFu is a very good language for an introduction to computer programming only because programs in such an introduction can be very simplistic.
If you would like to continue learning computer programming, we recommend our tutorial ZedLX, which is one of the easiest tutorials for learning computer programming.