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]
╰╴No statement matched
for number in numbers { print(number) }
thp

val dict = .{
╰╴No statement matched
apple: 10, banana: 7, cherries: 3, } for value in dict { print("{value}") }
thp

Loop over keys and values


val numbers = [0, 1, 2, 3]
╰╴No statement matched
for index, number in numbers { print("{index} : {number}") }
thp

val dict = .{
╰╴No statement matched
apple: 10, banana: 7, cherries: 3, } for key, value in dict { print("{key} => {value}") }
thp

While loop


val colors = ["red", "green", "blue"]
╰╴No statement matched
var index = 0 while index < colors.size() { print("{colors[index]}") index += 1 }
thp

Labelled loops

TBD

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


:top for i in values_1 {
╰╴No statement matched
for j in values_2 { // ... break :top } }
thp