Control Flow
if / else
With an else branch:
Chained conditions using else if:
if (score >= 90) {
console.println("A")
} else if (score >= 80) {
console.println("B")
} else if (score >= 70) {
console.println("C")
} else {
console.println("F")
}
The condition must be wrapped in parentheses. The body uses curly braces.
Truthiness
Any value can be used as a condition. The following are considered false:
false0(int)0.0(float)""(empty string)nil[](empty list)fixed([])(empty array)
Everything else is true.
while
The condition is checked before each iteration. If the condition is false from the start, the body never runs.
for ... in range
Iterate over a range of integers.
range with one argument
range(n) produces values from 0 to n - 1:
range with two arguments
range(start, end) produces values from start to end - 1:
for ... in (list iteration)
Iterate directly over the elements of a list:
break
Exit a loop early:
break exits only the innermost loop.
continue
Skip to the next iteration:
continue skips the rest of the current iteration and moves to the next one. In for loops, the loop variable is still incremented.