Control Flow

Conditionals, loops, and match expressions.

Control Flow

Sounio provides familiar control-flow constructs: if, while, for, and match.

if / else

if is an expression, so you can bind its result:

let max = if a > b { a } else { b }

while

fn main() with IO {
    var count = 0
    while count < 3 {
        println(count)
        count = count + 1
    }
}

for-in

Ranges:

fn main() with IO {
    // Exclusive range: 0..5
    var sum = 0
    for i in 0..5 {
        sum = sum + i
    }

    // Inclusive range: 0..=5
    var sum_inclusive = 0
    for i in 0..=5 {
        sum_inclusive = sum_inclusive + i
    }
}

Arrays and Vec<T>:

fn main() with IO {
    let arr = [10, 20, 30]
    for x in arr {
        println(x)
    }

    let vec: Vec<i32> = [1, 2, 3, 4]
    for x in vec {
        println(x)
    }
}

break and continue:

fn main() with IO {
    for i in 0..10 {
        if i % 2 == 0 {
            continue
        }
        if i >= 7 {
            break
        }
        println(i)
    }
}

match

fn main() with IO {
    let x = 1
    let y = match x {
        0 => 10
        1 => 20
        _ => 30
    }

    println(y)
}

Spec note: match guards, range patterns, and richer enum patterns are part of the language direction. Prefer the simplest match shape when writing portable examples.

What’s Next?