Control Flow
Conditionals, loops, and program flow statements.
Conditional Statements
Control program execution based on conditions using if,
elseif, and else.
Basic if/else
set age = 25
if (age >= 18) {
print("Adult")
}
if/elseif/else Chain
set score = 75
if (score >= 90) {
print("A")
} elseif (score >= 80) {
print("B")
} else {
print("C")
}
Use the single keyword elseif (not else if) for
additional conditions. Each branch is tested in order; the initial
if is required, while elseif branches and the final
else are optional. Conditions must evaluate to booleans; other
values are not implicitly treated as true or false.
While Loops
Execute a block repeatedly while a condition is true:
Simple Counter
set counter = 0
while (counter < 5) {
print(counter)
set counter = counter + 1
}
Sum 1 to 10
set sum = 0
set i = 1
while (i <= 10) {
set sum = sum + i
set i = i + 1
}
print(sum)
The loop repeats until the condition becomes false. Ensure your condition will eventually be false to avoid infinite loops.
for-in Loops
Iterate over arrays with for item in array,
or over object keys with for key, value in object.
Array Iteration
set items = [10, 20, 30]
for item in items {
print(item)
}
Object Iteration
set user = { name: "Kria", age: 5 }
for key, value in user {
print(key)
print(value)
}
Use one loop variable to iterate an array's values and two to iterate an object's keys and values. Object keys are visited in insertion order. An array loop reads elements as it advances; an object loop uses the keys present when iteration begins.
break & continue
Control loop execution with break (exit immediately)
and continue (skip to next iteration).
break Example
set i = 0
while (i <= 100) {
if (i >= 50) {
print(i)
break
}
set i = i + 1
}
continue Example
set nums = [1, 2, 3, 4, 5, 6]
for n in nums {
set remainder = n - ((n / 2) * 2)
if (remainder == 0) {
continue
}
print(n)
}
break exits the innermost loop.
continue jumps to the next iteration.