Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ballerina For Loop

Tags:

wso2

ballerina

Is there a for loop in ballerina like the for loop in java. I could only find foreach and while loop in the Ballerina Documentation.

    for(int i=0; i<10; i++){ 
        //some logic
    }
like image 975
Dan.M Avatar asked Oct 19 '25 03:10

Dan.M


1 Answers

Ballerina language has two looping constructs: while and foreach.

The while statement executes the while-block until the boolean expression evaluates to false.

The foreach statement iterates over a sequence of items. Executes the foreach-block for each and every item in the sequence.

Your requirement is to iterate over an ordered sequence of numbers. Ballerina support integer range expressions which create arrays of integers. e.g. 0...9 produces the range 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. You can find more on integer ranges here

Here is the foreach with integer ranges.

import ballerina/io;

function main (string... args) {
    foreach var i in 0...9 {
        io:println(i);    
    }   
}
like image 87
Sameera Jayasoma Avatar answered Oct 22 '25 03:10

Sameera Jayasoma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!