Mostly Harmless Docs/Control Structures

if $bladder-pressure > $critical {
    say "Be right back!";
}

# if/elif/else works like you'd expect, though the
# parentheses are no longer needed.

say "Looks good." if $things-look-good;
say "Just going out for a stroll." unless $tornado-outside;

# `while` works like you'd expect. As do last, next, and redo.

# Iterate over an array.
for @a -> $item {
    # Read like, "for each of @a as $item..."
    say $item;
}

# Can iterate of more than one at a time.
my @a = 1 .. 9;
for @a -> $i, $j, $k {
    say "($i, $j, $k)";
}
# prints:
# (1, 2, 3)
# (4, 5, 6)
# (7, 8, 9)

# Iterate over a hash.
for %h.kv -> $k, $v {
    say "$k: $v";
}

# C-style looping, if you need it.
loop {
    say "still looping..."
    last if rand < 0.5;
}

Main | Next: Scope