Velo by Example: While Loops

Velo has a single loop construct: while. There is no for loop, and no break or continue.

A basic while loop runs as long as the condition is true.

include "lang/terminal.vel";

int i = 1;
while (i <= 5) {
    term.println(i.str);
    i = i + 1;
};

Use compound assignment for concise loop counters.

int i = 0;
while (i < 10) {
    i += 1;
};

An infinite loop uses true as the condition. Since there is no break, use a boolean flag or modify the condition variable to exit.

bool running = true;
while (running) {
    # do work...
    running = false;  # exit the loop
};

To simulate early exit, set the loop counter to the limit.

int i = 0;
int n = 100;
while (i < n) {
    if (found) {
        i = n;    # exit the loop
    } else {
        i = i + 1;
    };
};