Swan Lake Beta2 is here.
import ballerina/io;

public function main() {
    float v1 = sum([10.5, 20.5, 30.5]);
    float v2 = sum2([10.5, 20.5, 30.5]);
    io:println("v1:", v1, " v2:", v2);
}

function sum(float[] v) returns float {
    float r = 0.0;
    // `foreach` statement can be used to iterate an `array`.
    // Each iteration returns an element in the `array`.
    foreach float x in v {
        r += x;
    }

    return r;
}

function sum2(float[] v) returns float {
    float r = 0.0;
    // `m ..< n` creates a value that when iterated over will give the
    // integers starting from `m` that are `< n`.
    foreach int i in 0 ..< v.length() {
        r += v[i];
    }

    return r;
}

Foreach Statement

The foreach statement iterates over an iterable value, by binding a variable to each member of the iterable value in order. foreach also works for strings, and will iterate over each character of the string.

import ballerina/io;
public function main() {
    float v1 = sum([10.5, 20.5, 30.5]);
    float v2 = sum2([10.5, 20.5, 30.5]);
    io:println("v1:", v1, " v2:", v2);
}
function sum(float[] v) returns float {
    float r = 0.0;
    foreach float x in v {
        r += x;
    }

foreach statement can be used to iterate an array. Each iteration returns an element in the array.

    return r;
}
function sum2(float[] v) returns float {
    float r = 0.0;
    foreach int i in 0 ..< v.length() {
        r += v[i];
    }

m ..< n creates a value that when iterated over will give the integers starting from m that are < n.

    return r;
}
bal run foreach_statement.bal
v1:61.5 v2:61.5
import ballerina/io;

public function main() {
    float v1 = sum([10.5, 20.5, 30.5]);
    float v2 = sum2([10.5, 20.5, 30.5]);
    io:println("v1:", v1, " v2:", v2);
}

function sum(float[] v) returns float {
    float r = 0.0;
    // `foreach` statement can be used to iterate an `array`.
    // Each iteration returns an element in the `array`.
    foreach float x in v {
        r += x;
    }

    return r;
}

function sum2(float[] v) returns float {
    float r = 0.0;
    // `m ..< n` creates a value that when iterated over will give the
    // integers starting from `m` that are `< n`.
    foreach int i in 0 ..< v.length() {
        r += v[i];
    }

    return r;
}

Foreach Statement

The foreach statement iterates over an iterable value, by binding a variable to each member of the iterable value in order. foreach also works for strings, and will iterate over each character of the string.

import ballerina/io;
public function main() {
    float v1 = sum([10.5, 20.5, 30.5]);
    float v2 = sum2([10.5, 20.5, 30.5]);
    io:println("v1:", v1, " v2:", v2);
}
function sum(float[] v) returns float {
    float r = 0.0;
    foreach float x in v {
        r += x;
    }

foreach statement can be used to iterate an array. Each iteration returns an element in the array.

    return r;
}
function sum2(float[] v) returns float {
    float r = 0.0;
    foreach int i in 0 ..< v.length() {
        r += v[i];
    }

m ..< n creates a value that when iterated over will give the integers starting from m that are < n.

    return r;
}
bal run foreach_statement.bal
v1:61.5 v2:61.5

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