Skip to content

Choices and loops

fn main {
let temperature: Int := 23
if temperature > 30 {
print("hot")
} else if temperature > 15 {
print("mild")
} else {
print("cold")
}
}
mild

The condition is a Bool, without parentheses; the braces are always written, even for one line, so adding a second line can never fall outside the if by mistake.

if also gives a value, which replaces the ? : of other languages:

fn sign(n: Int) -> String {
return if n < 0 { "negative" } else if n = 0 { "zero" } else { "positive" }
}
fn main {
print(sign(-4))
print(sign(0))
}
negative
zero

An if that gives a value needs an else: the value must exist in every case. A branch may also leave instead of giving one, with return, break or continue:

fn halved(n: Int) -> String {
let half: Int := if n % 2 = 1 { return "odd" } else { n / 2 }
return "half is {half}"
}
fn main {
print(halved(3))
print(halved(8))
}
odd
half is 4

When a value is compared with several possibilities, match lists them, one per line, each followed by => and what to do. Cases may be numbers, texts, characters, ranges, or several of them separated by commas; _ is anything else:

fn size(n: Int) -> String {
return match n {
0 => "none"
1..=9 => "small"
10, 20, 30 => "round"
_ => "big"
}
}
fn main {
print(size(4))
print(size(20))
print(size(512))
match "stop" {
"go" => print("going")
"stop" => print("stopping")
_ => print("unknown")
}
}
small
round
big
stopping

A match must cover every value: without _, a match on an Int is refused, and two cases that overlap (1..=9 and 5) are refused too, since the second one could never be reached. match really shines with enums, where the compiler knows every case.

The last case may be a new name instead of _: it takes every other value and names it.

fn main {
for n in [0, 7] {
match n {
0 => print("zero")
other => print("{other} is not zero")
}
}
}
zero
7 is not zero

A name that already exists is refused there: limit => would look like “equal to limit”, which it is not. Compare with if n = limit instead.

To decide on two values together, match a tuple of them. Each case is a tuple too, whose parts are values, ranges, _, or names that receive the part:

fn route(method: String, parts: List<String>) -> String {
return match (method, parts.length) {
("GET", 2) => "the list"
("GET", 3) => "note {parts[2]}"
("POST", 2) => "a new note"
(m, 2..=3) => "{m} is not allowed here"
(_, n) => "no page with {n} parts"
}
}
fn main {
print(route("GET", "/notes".split("/")))
print(route("GET", "/notes/7".split("/")))
print(route("DELETE", "/notes".split("/")))
print(route("GET", "/a/b/c".split("/")))
}
the list
note 7
DELETE is not allowed here
no page with 4 parts

The cases are tried in order, and the first that fits wins. The last one must take every value: _, or a tuple of names and _ like (_, n). Each part of the match is a copy, so method is still there after it.

while repeats as long as its condition is true:

fn main {
var n: Int := 1
while n < 100 {
n := n * 3
}
print(n)
}
243

for walks through a range of numbers, a list, a map or a set:

fn main {
for i in 0..3 {
write(i)
}
print("")
for i in 0..=3 {
write(i)
}
print("")
for i in 1..=3 {
write(i)
}
print("")
for name in ["Ada", "Alan"] {
print(name)
}
}
012
0123
123
Ada
Alan
  • 0..3 stops before 3, which suits positions in a list (0..l.length).
  • 0..=3 includes its end, which suits counting.
  • A range whose end is below its start is empty: 3..1 runs nothing. The ends are computed once, before the first turn.
  • write prints without going to the next line; print goes to the next line.

To go by another amount, or backwards, add step and a written number. A negative step needs a start above the end:

fn main {
for i in 0..10 step 3 {
write(i)
}
print("")
for i in 3..=1 step -1 {
write(i)
}
print("")
}
0369
321

With two names, for also gives each element’s place, counted from 0:

fn main {
for i, name in ["Ada", "Alan"] {
print("{i}: {name}")
}
}
0: Ada
1: Alan

Outside a for, a range is a List<Int> of its numbers, so every list method works on it. In a for, no list is made: for i in 1..1000000000 costs nothing, while (1..1000000000).length would really write those numbers in memory.

Parentheses around the name take a tuple apart at each turn, with _ for a part you don’t need. They work in the two-name forms too: for i, (name, _) in people.

fn main {
let squares: List<Int> := (1..=4).map { x: Int => x * x }
print(squares)
let people: List<(String, Int)> := [("Ada", 36), ("Alan", 41)]
for (name, age) in people {
print("{name}: {age}")
}
}
[1, 4, 9, 16]
Ada: 36
Alan: 41

These parts are copies. To change the elements, write for x in var l and use x.0.

break leaves the loop at once, continue goes to its next turn:

fn main {
var total: Int := 0
for i in 1..=10 {
if i % 2 = 0 {
continue
}
if i > 7 {
break
}
total += i
}
print(total)
}
16

(1 + 3 + 5 + 7.) return leaves the whole function, from inside any loop.