#8: Loops
Loops allow you to repeat sections of code.
They can be started with the [ token and end with the ] token, and they will repeat until the top value in the stack is 0: that’s why, when using the Input operator, a 0 gets pushed to the stack!
Let’s look at an example:
1010>:;
[
:>';
:!;
'>#;
'-1;
'>:;
] this code puts a 10 in the stack, then in the loop it does the following:
- Copies the value in the stack into register 0 and pops it from the stack;
- Prints the numeric value of register 0;
- Subtracts
1from register 0; - Pushes the value of register 0 into the stack.
So, at one point, at the end of the loop the top value in the stack is going to be 0, making the loop end!
Remember that “mirror” program that printed back the first character the user inputs? Try to rewrite it with loops so that it prints back the whole input!