[Home]Brainfuck programming language/Examples

HomePage | Brainfuck programming language | Recent Changes | Preferences

Character I/O
,.
Take one character from the keyboard and output it to the screen.

Simple loop
+[,.]
A continuous loop that takes keyboard input and echoes it to the screen. Note that the reason that the + is here incrementing the first byte value, even though it gets overwritten immediately by our input, is to ensure that the loop we created can start. Had we not set the pointer to some nonzero value (since it is initially zero) the loop would not have started in the first place.

Pointer manipulation
+[,.>+]
a continuous loop that puts keyboard input into our pointer, outputs it to the screen, then increments our pointer location. This has an advantage over the previous loop, because it is saving our keystrokes into the character array for some theoretical future use. The loop before simply overwrote the same initial byte in the array because it never moved the pointer. Again, note the plusses. The first again allows the loop to begin. The second increments the value at the location we just moved the pointer to, ensuring that when the close bracket jumps back to open bracket, the loop will continue because our pointer references a nonzero value.

Additional examples here

Conditional statements
+[,-------------[-------------------.[-]>+<]>]
This will take lowercase input from the keyboard and make it uppercase. To exit, press you press the enter key. Lets look at this program. Pretty complex for such a simple task, and it doesn't even do any data validation! the bulk of the visible work it's doing is subtracting 32d from the pointer reference before its output. This is because ASCII uppercase letters are offset -32 from lowercase. But let's look at the whole thing.

First, we increment our initial pointer to kickstart our loop. We take keyboard input using the (,) and immediately subtract 13. ASCII 13 is the carriage return. If the user hit enter, the next command, a loop declaration (]) will not run because we effectively set our pointer ref to zero! We will deal with the pointer increment after this loop soon, but let's examine what happened if the user didn't hit enter, and pressed a valid lowercase letter: So the internal code block is run--first, it decrements the pointer reference by 19. This is because we have already decremented our value by 13. 13 + 19 equals 32. We have changed our value to uppercase.

Next we output it. Now we have the curious code ([-]). This is a loop that simply runs our value down to zero without having to know ahead of time what it already was. Bear with me, this becomes important. Now we increment the pointer, add 1 to the value, and decrement the pointer back again, effectively writing a 1 to the location ahead of the pointer location. Now we jump back to our corresponding open bracket, the second one from the left. the code block is now ignored, because we set the value to zero.

Now we go to the next command, the pointer increment. recall that we incremented this value to 1 (making it a nonzero), so when the return bracket jumps back to the beginning code block, it is repeated.

Right now this program has a kind of memory leak. Every time the outer loop runs, the pointer is incremented one time from the last. Eventually, it can run out of memory. Maybe I'll fix this later, but it's only an example!


HomePage | Brainfuck programming language | Recent Changes | Preferences
This page is read-only | View other revisions
Last edited September 11, 2001 12:57 am by 65.6.45.xxx (diff)
Search: