While loops

While loops in XY are very similar to loops in C or Python. Major difference is loops in XY are expressions i.e. result in a value. Here is the general syntax:

while (cond) -> (res := initValue) {
    ... body ...
}

This is equivalent to

-> (res := initValue) {
    while (cond) {
        ... body ...
    }
}

Semantics

While cond is true, body is executed.

The variable res can be used in cond despite the fact its declaration appears after the cond espression.

Do-While loops

They are very similar to while loops except the body is first executed and only then the condition is checked.

do -> (res := initValue) {
    ... body ...
} while (cond);

is equivalent to

-> (res := initValue) {
    do {
        ... body ...
    } while (cond);
}

A terminating semicolon is always required at the end of a do-while.

Named loops

Loops can be given a name. The name can serve two purposes:

  • Add additional information what the loops is supposed to do
  • Be used to break or continue the loop from within another loop
while loopName(cond) {
    ... body ...
}

do {
    ... body ...
} while loopName(cond);

Break

The keyword break can be used to skip the remaining code in the current iteration and move to executing the code immediatelly after the loop. Alternatively the break keyword may be followed by the name of the loop to break.

do {
    while inner(cond2) {
        if (cond3) {
            break outer;
        }
    }
} outer(cond1);

Continue

The keyword continue can be used to skip the remaining code in the current iteration and move to reevaluating the condition. A specific loop to continue can be specified by using its name.

do {
    while inner(cond2) {
        if (cond3) {
            break outer;
        }
    }
} outer(cond1);

This site uses Just the Docs, a documentation theme for Jekyll.