Hello World

Traditionally the first program you would write in a new language is one that prints "Hello, world" (c.f. D.W.). This is no mean feat in BF, mostly beacause of the difficulty in producing large constants. The only textual output we have is the "." which prints an ASCII char. So if we could fill the cells with the right values we can print any string we want.

For brevity, I will demonstrate a shortened program which prints "hi", but an interested reader should be able to extend this to "Hello, world" if they were so inclined. The ASCII code for "hi" is {104,105}.

//print "hi"
set A, B = 10
multiply A * B => D
add 4 to D
print D
add 1 to D
print D
Ok, here goes:
++++++++++ > ++++++++++
[< [>>+>+<<<-] >> [<<+>>-] <-] >>
++++ .
+ .

Conditional Execution

So far we can add and multiply - but any Turing complete language must support conditional execution, that is the ability to make decisions based on data that is not known at the time of writing the program.

The only way of doing this in BF is using the [ ] brackets, which act like a "while non-zero" statement in a boring language like C. How would you go about simulating an "if" statement? The main difficulty is producing an "if (x=0)" statement, using only "while (x!=0)" statements. A useful trick is to use a blank temp cell to turn a while into an if:

//if (A == 0) print 5
//label cells A, tmp, flg
input A
set flg to 1
if (A != 0) clear flg, move A to tmp
move tmp to A    //does nothing if A == 0
if (flg != 0) print 5
; >>+<< [ >>-<< [>+<-] ] > [<+>-] > [ ++++ : [-] ] <<

A Harder Function

Lets consider how you would program the modulo function in BF.

(The modulo function, written mod, is the remainder after integer division. So for example 16 mod 13 = 3 and 6 mod 13 = 6.)

An outline of the mod function in pseudo-code:

//output A mod B
input A, B
copy B into C
while (A != 0)
  A--; B--
  if B = 0
    copy C into B
  if A = 0
    set ans = C - B
output ans
Lets try and get this beast into BF. I think this one needs lots of comments. I find the most helpful comments are those that remind you where the pointer has got to. Recall that characters that are not BF commands are ignored as comments by the interpreter.
call the cells ans~a~b~c~tmp 
get a & b
> ; > ;
copy b into b and c & point at a
[>+>+<<-]>>[<<+>>-]<<<
while(a) [
decr(a & b) and point at c
->->
"if(b=0)"   
  put tmp=1  &  point at b
  >[-]+<<  
  if b!=0 clear tmp and move b to ans
  [>>[-]<<[<<+>>-]]
  move ans to b
  <<[>>+<<-]>>
  if(tmp!=0) copy c into b & tmp   copy tmp into c
  >> [-<[<+>>+<-]>          [<+>-]] <
endif  and we are pointing at c again
"if(a=0)"
put tmp =1 and point at a
  >[-]+<<<
  if a!=0 clear tmp and move a to ans
  [>>>[-]<<<[<+>-]]
  move ans to a
  <[>+<-]>
  if(tmp!=0) set ans = c minus b and clear abc&tmp
  >>>[-<[<<<+>>>-]<[<<->>-]<[-]>>>]<
endif pointing at c
point at a
<<]
end while(a)
output ans
<:

Of course, it's much more compact without the comments:

> ; > ;
[>+>+<<-]>>[<<+>>-]<<<
[
->->
  >[-]+<<  
  [>>[-]<<[<<+>>-]]
  <<[>>+<<-]>>
  >> [-<[<+>>+<-]>          [<+>-]] <
  >[-]+<<<
  [>>>[-]<<<[<+>-]]
  <[>+<-]>
  >>>[-<[<<<+>>>-]<[<<->>-]<[-]>>>]<
<<]
<:

I hope this has given you some ideas and helped you get started with BrainFuck. Happy programming.