For loops

Home of the ABS programming language: the joy of shell scripting.


For loops

ABS supports 2 types of for loops: the “standard” form and the “in” one.

Standard form

A standard for loop takes the canonical form:

for x = 0; x < 10; x = x + 1 {
    echo("Looping...")
}

It’s important to understand scoping in this form: if the loop declares an identifier that was already declared earlier, it will be “temporarely” overwritten in the loop, but will assume its original value afterwards.

Code is better than 1000 words:

x = "hello world"

for x = 0; x < 10; x = x + 1 {
    # x is 0, 1, 2...
}

echo(x) # "hello world"

Similarly, a variable declared on the loop (not inside) will cease to exist after the loop is done:

for x = 0; x < 10; x = x + 1 {
    # x is 0, 1, 2...
}

echo(x) # x is not defined here

Finally, variables declared inside the loop will instead keep living afterwards:

for x = 0; x < 10; x = x + 1 {
    y = x
}

echo(y) # 9

In form

The “in” form of the for loops allows you to iterate over an array or an hash:

for x in [1, 2, 3] {
    # x is 1, 2, 3
}

for x in {"a": 1, "b": 2, "c": 3} {
    # x is 1, 2, 3
}

Both key and values are available in the loop:

for k, v in [1, 2, 3] {
    # k is 0, 1, 2
    # v is 1, 2, 3
}

for k, v in {"a": 1, "b": 2, "c": 3} {
    # k is a, b, c
    # v is 1, 2, 3
}

In terms of scoping, the “in” form follows the same rules as the standard one, meaning that:

k = "hello world"

for k, v in [1, 2, 3] {
    # k is 0, 1, 2
    # v is 1, 2, 3
}

echo(k) # "hello world"
echo(v) # v is not defined

For … else …

For loops can also have else clause which executes if the list in the for condition is empty.

For example, when we run a database query like the following, if the “users” list is empty, we will only execute the statements inside the else clause.

users = db.query("SELECT students WHERE age > 20")

for user in users {
  print(user)
} else {
  print("We don't have students above the age of 20")
}

Next

That’s about it for this section!

You can now head over to read about while loops.