I tried out this hello world program in Brainfuck. How can I print the text multiple number of times? Here's my code:
+++++++[>++++++++++ <- ] >++.>++++++[>++++++++++ <- ] >+++++++++.>+++++++[>++++++++++ <- ] >++++++..>+++++++[>++++++++++ <- ] >+++++++++.>+++[>++++++++++ <-]>++. >++++++++[>++++++++++<-]>+++++++.>+++++++[>++++++++++<-] >+++++++++.>++++++++[>++++++++++ <-]>++.>+++++++[>++++++++++ <- ] >++++++.>++++++[>++++++++++ <-]>++++++++.>+++[>++++++++++<-]>++.>+++[>++++++++++<-]>+++.>+++[>++++++++++<-]>+++.
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.
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--
] }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With