Swan Lake Beta3 is here.
import ballerina/io;

public function main() {
    LinkedList link1 = {value: "link1", next: ()};
    LinkedList link2 = {value: "link2", next: link1};
    io:println(len(link2));
}

type LinkedList record {
    string value;
    LinkedList? next;
};

function len(LinkedList ll) returns int {
    int n = 0;
    LinkedList? v = ll;
    // Executes the code block that is defined within the `while` block
    // as long as the value of `v` is not `nil`.
    while v != () {
        n += 1;
        v = v.next;
    }

    return n;
}

While Statement

The while statement is a more flexible iteration than foreach. break and continue statements can be used within the loops to alter control flow.

import ballerina/io;
public function main() {
    LinkedList link1 = {value: "link1", next: ()};
    LinkedList link2 = {value: "link2", next: link1};
    io:println(len(link2));
}
type LinkedList record {
    string value;
    LinkedList? next;
};
function len(LinkedList ll) returns int {
    int n = 0;
    LinkedList? v = ll;
    while v != () {
        n += 1;
        v = v.next;
    }

Executes the code block that is defined within the while block as long as the value of v is not nil.

    return n;
}
bal run while_statement.bal
2
import ballerina/io;

public function main() {
    LinkedList link1 = {value: "link1", next: ()};
    LinkedList link2 = {value: "link2", next: link1};
    io:println(len(link2));
}

type LinkedList record {
    string value;
    LinkedList? next;
};

function len(LinkedList ll) returns int {
    int n = 0;
    LinkedList? v = ll;
    // Executes the code block that is defined within the `while` block
    // as long as the value of `v` is not `nil`.
    while v != () {
        n += 1;
        v = v.next;
    }

    return n;
}

While Statement

The while statement is a more flexible iteration than foreach. break and continue statements can be used within the loops to alter control flow.

import ballerina/io;
public function main() {
    LinkedList link1 = {value: "link1", next: ()};
    LinkedList link2 = {value: "link2", next: link1};
    io:println(len(link2));
}
type LinkedList record {
    string value;
    LinkedList? next;
};
function len(LinkedList ll) returns int {
    int n = 0;
    LinkedList? v = ll;
    while v != () {
        n += 1;
        v = v.next;
    }

Executes the code block that is defined within the while block as long as the value of v is not nil.

    return n;
}
bal run while_statement.bal
2
Subscribe to the newsletter

In the creation of Ballerina, we were inspired by so many technologies. Thank you to all that have come before us (and forgive us if we missed one): Java, Go, C, C++, D, Rust, Haskell, Kotlin, Dart, TypeScript, JavaScript, Python, Perl, Flow, Swift, Elm, RelaxNG, NPM, Crates, Maven, Gradle, Kubernetes, Docker, Envoy, Markdown, GitHub and WSO2.

Cookie Policy

This website uses cookies so that we can provide you with the best user experience. Read our Cookie Policy to find out more.

If you wish to disable cookies you can do so from your browser.

I Understand