import ballerina/io;

public function main() {
    int i = 0;
    // This is a basic `while` loop.
    while (i < 3) {
        io:println(i);
        i = i + 1;
    }

    int j = 0;
    while (j < 5) {
        io:println(j);
        j = j + 1;

        // The `break` statement can be used to break the loop.
        if (j == 3) {
            break;
        }
    }

    int k = 0;
    while (k < 5) {
        // The `continue` statement can be used to move to the
        // next loop iteration immediately.
        if (k < 3) {
            k = k + 1;
            continue;
        }

        io:println(k);
        k = k + 1;
    }
}

While

A while loop statement repeatedly executes the code block that is defined within the while block as long as a given condition is met.

import ballerina/io;
public function main() {
    int i = 0;
    while (i < 3) {
        io:println(i);
        i = i + 1;
    }

This is a basic while loop.

    int j = 0;
    while (j < 5) {
        io:println(j);
        j = j + 1;
        if (j == 3) {
            break;
        }
    }

The break statement can be used to break the loop.

    int k = 0;
    while (k < 5) {
        if (k < 3) {
            k = k + 1;
            continue;
        }

The continue statement can be used to move to the next loop iteration immediately.

        io:println(k);
        k = k + 1;
    }
}
# To run this sample, navigate to the directory that contains the
# `.bal` file, and execute the `ballerina run` command.
ballerina run while.bal
0
1
2
0
1
2
3
4