Control flow
Goblin uses braces for blocks and does not need semicolons.
An indented style makes nested blocks easy to scan, but indentation is not part of the grammar: braces determine the block.
Conditions
Use if, else if, and else to select a branch:
if score >= 90 {
print("excellent")
} else if score >= 60 {
print("passed")
} else {
print("try again")
}
Every condition is evaluated with the truthiness rules described in Built-in types. This lets you test optional values and collections directly:
var users = []
if users {
print("have users")
} else {
print("no users")
}
while loops
A while loop repeats while its condition is true. break leaves the nearest
loop and continue skips to its next iteration.
var n = 0
while n < 10 {
n = n + 1
if n == 3 {
continue
}
if n == 6 {
break
}
print(n)
}
Be sure that a while condition eventually changes or that the loop reaches a
break; Goblin does not impose a loop limit. In nested loops, break and
continue apply only to the innermost loop.
for ... in loops
for iterates over lists, strings, dictionaries, and other iterable values.
Iterating over a dictionary produces its keys, in insertion order.
for name in ["Ada", "Linus"] {
print(name)
}
for i in range(3) {
print(i) # 0, 1, 2
}
for i in range(1, 4) {
print(i) # 1, 2, 3
}
range(end) creates the integers from zero up to, but excluding, end, and
range(start, end) those from start. It also accepts named arguments, such
as range(start=0, end=3). Called directly in a loop header, range counts
instead of building its list, so for i in range(1000000000000) with an early
break costs only the iterations that run.
A loop takes one element at a time from the value it walks. For a file that
means one line at a time (see fs): the loop reads only as far
as it gets, and an error such as an invalid line is raised when the loop
reaches it, after the earlier elements have run through the body. A list is
walked in place, by position, over at most as many elements as it held when
the loop started: assigning an element the loop has not reached yet is seen,
elements pushed during the loop are not visited, and when pop, remove or
clear has made the list shorter than the position the loop reached, the
loop ends there. The list is not copied, so a loop costs nothing up front.
Every other walk over a list follows the same rule, such as the function
given to map, filter, find or max(key=). A dictionary loop visits the
keys it held when the loop started.
However a loop ends (it runs out of elements, break, a return in its body,
or an error leaving it) the value being walked is told once that the loop is
done, before anything after the loop runs, the function's deferred calls
included; a return evaluates its value first. A value that acquired
something for the loop, such as a sequence that opened a file to read it,
releases it then. A file you opened yourself and loop over stays open: it
belongs to you, not to the loop.
Strings iterate by character, lists by element, and dictionaries by key:
var scores = {"Ada": 10, "Linus": 9}
for name in scores {
print(name, scores[name])
}
for character in "go" {
print(character)
}
A dictionary is iterated in insertion order: the order its keys were first written in.
When every element is itself a small sequence, name its parts in the loop
header. Each element is destructured as var a, b = element would be, so an
element with a different number of parts raises ValueError:
for name, score in scores.items() {
print(name, score)
}
for x, y in [[0, 1], [2, 3]] {
print(x + y)
}