-
Notifications
You must be signed in to change notification settings - Fork 0
repeat
The repeat loop repeats an action a certain number of times. It is an alternative to the while loop that can be used when the number of repetitions is known. Here is an example of code:
$a = 0;
repeat (20) {
print(a=a+1)
};
print("Done");
The above code will print the numbers 1 through 20, and print out "Done" at the very end.
The number of repetitions can also be a variable, as shown below:
$i = 20;
$a = 0;
repeat (i) {
print(a=a+1)
};
print("Done");
This code will perform the same function. However, it's best practice not to declare unnecessary variables in order to keep one's code clean.
The repeat loop also supports infinite loops when no parameter is supplied for the repetitions:
$a = 0;
repeat {
print(a=a+1)
};
print("Done");
This code will continuously print larger and larger numbers until the 32-bit integer limit is reached. However, it is strongly advised not to include these unsparingly as they can quickly consume a large amount of system memory. Of course, infinite loops have their applications where they may be used.
- Output
- Input
- Variables
- Loops and Conditionals
- Mathematics
- Quadratics
- Cubics
- Complex Numbers
- Trigonometry
- Miscellaneous