Version: v0.5.1 - Beta.  We welcome contributors & feedback.  THanks!


For Loops 

A for loop repeats the same block of code multiple times.

They are most commonly used to iterate over a List.

let colors = ['Red', 'Blue', 'Yellow'];

// 'color' holds the value of the current
// element in the loop.
for (color in colors) {
    print(color ~ ' is a primary color.');
//= 'Red is a primary color'
//= 'Blue is a primary color'
//= 'Yellow is a primary color'

// Loop over a range of numbers
for (n in range(1, 3)) {
//= 1
//= 2
//= 3

Looping Over a Map

You can also loop over the key/value pairs of a Map.

Use a colon : to create a variables for the current key and value.

let countryCodes = {
    US: 'United States',
    FR: 'France',
    JP: 'Japan',

for (code:name in countryCodes) {
    print(code ~ ' is the code for ' ~ name);
//= US is the code for United States
//= FR is the code for France
//= JP is the code for Japan

Loop Control

Use break to immediately exit a loop. The program will jump to the end of the block and resume.

Use continue to skip the current cycle of a loop. It will immediately jump to the top of the block and resume the next cycle.

let lines ='quotes.txt');

// Find the 1st quote that refers to 'inspiration'
for (line in lines) {
    if (line.contains('inspiration')) {
        break;  // end the loop

Forever Loops

A bare for loop will keep repeating until you exit via break.

This is useful when you don’t know how many times it will repeat.

// Print all quotes that refer to 'creativity'
let file ='quotes.txt');
for {
    let line = file.readLine();
    if (line == false) {
        break; // end of file
    else if (line.contains('creativity')) {

JargonOther languages achieve this with the while and do/while commands. THT's approach is simpler and avoids some common pitfalls with those commands (such as off-by-one bugs).