Loops
Edit on GitHubLoops allow you to repeat an action over and over (and over).
While Loops
The most basic loop is the while loop. The code within the loop repeats until the condition is false.
Loops can skip to the next iteration using the continue keyword. For example, to print only the odd numbers from zero to ten:
Use the break keyword to exit from a loop entirely:
For Loops
for loops are just like while loops, just with more structure. for loops accept an optional initializer statement which runs before the loop, an optional condition statement which runs before each iteration, and an optional increment statement which runs after each iteration.
for loops are commonly used to iterate arrays:
1 | module Main |
Since all of the loop parameters are optional, they can all be omitted (though the semicolons are still required). For example, here’s how we can define an infinite loop: