A Tour of Morfa

Break and continue

Statements break and continue may be used inside any kind of loop statement; break exits the loop immediately whereas continue skips the rest of the loop body and restarts the iteration for the next value (if any).

import morfa.io.print;
import morfa.util.Range;

unittest
{
    for (n in 0..102)
    {
        if (n > 100)
            break;

        if (n % 15 == 0)
        {
            println("fizzbuzz");
            continue;
        }
        if (n % 3 == 0)
        {
            println("fizz");
            continue;
        }
        if (n % 5 == 0)
        {
            println("buzz");
            continue;
        }
        printf("{}\n", n);
    }
}

In order to break from or continue a loop that is not immediately enclosing the break or continue statement, you may attach a label to the statement and then refer to this label with break or continue.

unittest
{
    // The for loop below is labelled `OUTER`
    OUTER: for (i in 1..11)
    {
        for (j in 1..11)
        {
            printf("{},{}\n", i, j);
            if (j == i)
                continue OUTER;
        }
    }
}