Choices and loops
fn main { let temperature: Int := 23 if temperature > 30 { print("hot") } else if temperature > 15 { print("mild") } else { print("cold") }}mildThe 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))}negativezeroAn 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))}oddhalf is 4match on a value
Section titled “match on a value”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") }}smallroundbigstoppingA 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") } }}zero7 is not zeroA name that already exists is refused there: limit => would look like “equal to limit”, which
it is not. Compare with if n = limit instead.
Several values at once
Section titled “Several values at once”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 listnote 7DELETE is not allowed hereno page with 4 partsThe 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)}243for and ranges
Section titled “for and ranges”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) }}0120123123AdaAlan0..3stops before 3, which suits positions in a list (0..l.length).0..=3includes its end, which suits counting.- A range whose end is below its start is empty:
3..1runs nothing. The ends are computed once, before the first turn. writeprints without going to the next line;printgoes 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("")}0369321With two names, for also gives each element’s place, counted from 0:
fn main { for i, name in ["Ada", "Alan"] { print("{i}: {name}") }}0: Ada1: AlanRanges as values, tuples taken apart
Section titled “Ranges as values, tuples taken apart”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: 36Alan: 41These parts are copies. To change the elements, write for x in var l and use x.0.
break and continue
Section titled “break and continue”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.