Loops


Loop

While loop

A while loop performs a set of statements until a condition becomes false. These kind of loops are best used when the number of iterations is not known before the first iteration begins.

    func main() {
        var i = 0;

        while (i < 50000) {
            i += 1;
        }

        return i;
    }

Repeat-while loop

The other variation of the while loop, known as the repeat-while loop, performs a single pass through the loop block first, before considering the loop’s condition. It then continues to repeat the loop until the condition is false.

    func main() {
        var i = 0;

        repeat {
            i += 1;
        } while (i < 50000);

        return i;
    }

For loop

You can access an element from a list by calling the subscript operator [] on it with the index of the element you want. As in most languages, indices start at zero:

    var count = 0;
    for (var i in 0...40) {
        count += i;
    }
    return count;

The for in loop can be used over any object that supports iteration, such as Lists, Strings or Maps.

Loop method

Performing a loop is very common operation in any programming language, so Gravity adds a very convenient way to run a loop by adding a special loop method to some classes (Int, Range, List, String and Map) that accepts a closure as parameter:

    func main() {
        4.loop({System.print("Hello World");});
    }
    // Output:
    // Hello World
    // Hello World
    // Hello World
    // Hello World

If we need to access the current index of the loop we can just rewrite the closure:

    func main() {
        var target = 5;
        target.loop(func (value){System.print("Hello World " + value);});
    }
    // Output:
    // Hello World 0
    // Hello World 1
    // Hello World 2
    // Hello World 3
    // Hello World 4

Loop within a Range:

    func main() {
        var target = 0...4;
        target.loop(func (value){System.print("Hello World " + value);});
    }
    // also in reverse order
    func main() {
        var target = 4...0;
        target.loop(func (value){System.print("Hello World " + value);});
    }

Loop within a Lists:

    func main() {
        var target = [10,20,30,40,50,60,70,80,90];
        target.loop(func (value){System.print("Hello World " + value);});
    }

Loop within a String:

        func main() {
                var s = "abcdefghijklmnopqrstuvwxyz";

                var vowels = ""
                s.loop(func (c) {
                        if (c == "a" or
                            c == "e" or
                            c == "i" or
                            c == "o" or
                            c == "u") {
                                vowels += c;
                        }
                })
                System.print(vowels)  // aeiou
        }

Loop within a Maps where the key is passed as closure argument (please note that key order is not preserved):

    func main() {
        var target = ["key1":10,"key2":20,"key3":30,"key4":40];
        target.loop(func (key){System.print(key);});
    }
    // Output:
    // key1
    // key2
    // key4
    // key3