Language
Control Flow
print, if, for, and the repeat loops
Statements are evaluated for their side effects. Control-flow statements decide what runs and how often.
Quick reference
print(x)
if cond print('foo') else print('bar') end
for x in 'foo' print(x) end
for x in 'foo' index i print(i) end
repeat 3 times print(it) end
repeat while x < 3 x = x + 1 end
repeat until x >= 3 x = x + 1 end
break
continue
print(x)
if
if true
print('foo')
else
print('bar')
end
The else branch is optional:
if true
print('foo')
end
for
for iterates over any iterable: lists, strings (per character), and maps.
for x in 'foo'
print(x)
end
That prints f, o, o on separate lines.
The optional index clause binds the iteration index:
for x in 'foo' index i
print(i)
print(x)
end
The loop variable does not escape the loop - see Variables & Scope.
repeat
repeat has three forms. repeat N times runs a block N times, binding the
current count to it:
repeat 3 times
print(it)
end
repeat while runs as long as its predicate holds; repeat until runs until the
predicate becomes true:
repeat while x < 3
x = x + 1
end
repeat until x >= 3
x = x + 1
end
The condition and body can share a line (repeat while x < 3 x = x + 1 end),
but putting the body on its own line reads more clearly.
Inside any loop, break exits and continue skips to the next iteration:
break
continue
Blocks
A block is a sequence of statements terminated by end. The if, for, and
repeat forms above all use blocks.
See also
- Variables & Scope for how loop variables are scoped.
- Operators & Expressions for the expression form of conditionals.
- Functions & Closures for inline functions.