Control flow
Conditional execution and collection iteration.
Conditional branches
conditions.sar
func status(health int32, alive bool) string {
if alive and health > 50 {
return "ready"
} else if alive and health > 0 {
return "hurt"
} else {
return "stopped"
}
}Conditions must have type bool. Branches useif, optional else if branches, and an optional final else.
While loops
A while loop repeats while its boolean condition is true. Sarlin evaluates the condition before every iteration.
For loops
loops.sar
func main() {
local var count int32 = 0
while count < 5 {
count = count + 1
if count == 2 {
continue
}
print(count)
}
local var names string[] = ["Bob", "Alice"]
for name, position in names {
print(position + ": " + name)
}
}A for loop iterates an array or dictionary. For arrays, the first name receives the value and an optional second name receives its zero-based position. For dictionaries, the first name receives the value and the optional second name receives the key.
Break and continue
break exits the nearest loop. continueskips to its next iteration. Both are valid only inside while orfor loops.