Control flow
Making decisions
Section titled “Making decisions”if runs a block only when its condition is true. else if tries another condition, and else runs when none
matched.
fn describe(light) { if light < 4 { return "dark" } else if light < 10 { return "dim" } else { return "bright" }}
fn main() { print(describe(2)) print(describe(8)) print(describe(15))}darkdimbrightThe condition must be true or false. Keep the opening { on the same line as if. else may start on the line
after the closing }.
Repeating with while
Section titled “Repeating with while”while repeats its block as long as the condition stays true. It is the way to count:
fn main() { countdown = 3 while countdown > 0 { print(countdown) countdown-- } print("Go!")}321Go!Going through a collection with for
Section titled “Going through a collection with for”for item in collection runs the block once for each element of a list or tuple, or each key of a dictionary.
fn main() { for mob in ["zombie", "skeleton", "creeper"] { print("Watch out for the " + mob) } prices = {iron: 3, gold: 8} for name in prices { print(name + " costs " + str(prices[name])) }}Watch out for the zombieWatch out for the skeletonWatch out for the creeperiron costs 3gold costs 8The loop works on a snapshot taken when it starts, so adding or removing elements inside the loop does not change
which elements it visits. There is no range; use while with a counter to repeat a number of times. Strings cannot
be looped over.
break and continue
Section titled “break and continue”break leaves the loop at once. continue skips to the next round.
fn main() { for n in [1, 2, 3, 4, 5, 6] { if n % 2 is 0 { continue } if n > 4 { break } print(n) }}13A variable created inside a block, including a loop or if block, exists only inside it. The loop variable of for
belongs to the loop body. Assigning to a name that already exists outside changes that outer variable instead of
making a new one.
Leaving a function
Section titled “Leaving a function”return ends the function immediately, optionally with values. See functions.