Loops

For loop

THP loops are similar to PHP’s foreach. There is no equivalent to PHP’s for.

Braces are required.

Loop over values

val numbers = [0, 1, 2, 3]

for number in numbers
{
    print(number)
}thp
    
val dict = .{
    apple: 10,
    banana: 7,
    cherries: 3,
}

for value in dict
{
    print("{value}")
}thp
    
Syntax error: Expected an expression after the equal `=` operator at line 1:9

Loop over keys and values

val numbers = [0, 1, 2, 3]

for index, number in numbers
{
    print("{index} : {number}")
}thp
    
val dict = .{
    apple: 10,
    banana: 7,
    cherries: 3,
}

for key, value in dict
{
    print("{key} => {value}")
}thp
    
Syntax error: Expected an expression after the equal `=` operator at line 1:9

While loop

val colors = ["red", "green", "blue"]
var index = 0

while index < colors.size()
{
    print("{colors[index]}")
    index += 1
}thp
    
Syntax error: Expected a block after the condition, found . at line 4:21

Labelled loops

TBD

You can give labels to loops, allowing you to break and continue in nested loops.

:top for i in values_1
{
    for j in values_2
    {
        // ...
        break :top
    }
}thp
    
Syntax error: Expected an statement or an expresion at the top level. at line 1:0