8

I tried out this hello world program in Brainfuck. How can I print the text multiple number of times? Here's my code:

+++++++[>++++++++++ <- ] >++.>++++++[>++++++++++ <- ] >+++++++++.>+++++++[>++++++++++ <- ] >++++++..>+++++++[>++++++++++ <- ] >+++++++++.>+++[>++++++++++ <-]>++. >++++++++[>++++++++++<-]>+++++++.>+++++++[>++++++++++<-] >+++++++++.>++++++++[>++++++++++ <-]>++.>+++++++[>++++++++++ <- ] >++++++.>++++++[>++++++++++ <-]>++++++++.>+++[>++++++++++<-]>++.>+++[>++++++++++<-]>+++.>+++[>++++++++++<-]>+++.
MattAllegro
  • 6,455
  • 5
  • 45
  • 52

2 Answers2

7

Let's think of a 5 character long word like "hello".

So if you want it to print those 5 characters 3 times you could have a code like this:

,>,>,>,>,>+++[<<<<<.>.>.>.>.>-]

Let me explain the code:

The first part of the code is the input part:

,>,>,>,>,

Then you initialize a variable containing the information that you want to print it 3 times.

>+++

Then you have the loop which goes back to the start, prints out those 5 characters, and goes to the variable and decrement it.

[<<<<< //goes back

.>.>.>.>. //print out

>-]  //decrement

If you got the idea, then you can easily improve the code by e.g. putting more loops in it, I just wanted to show you a simple idea.

Leah
  • 458
  • 3
  • 15
4

A more general answer is as follows.

Let's say you want to print the text 5 times (we'll call this counter)

+++++ >

and then import characters until a \n (= 10) is given.

----- -----[+++++ +++++ >, ----- -----]

The array now looks like < counter, 0, string >. Finally, we place the pointer at counter.

<[<]<

We finish by printing the string multiple times.

[       while (counter) {
>>[.>]    print string
<[<]<-    counter--
]       }
JorisH
  • 428
  • 2
  • 8