Skip to main content
Kira

Control Flow

Control flow constructs in Kira are statements, not expressions.

If Statements

fn max(a: i32, b: i32) -> i32 {
    if a > b {
        return a
    }
    return b
}

fn sign(n: i32) -> string {
    var result: string = ""
    if n > 0 {
        result = "positive"
    } else if n < 0 {
        result = "negative"
    } else {
        result = "zero"
    }
    return result
}

For Loops

For loops iterate over collections:

fn sum_list(items: List[i32]) -> i32 {
    var total: i32 = 0
    for item in items {
        total = total + item
    }
    return total
}

While Loops

var i: i32 = 0
while i < 10 {
    std.io.println(to_string(i))
    i = i + 1
}

Loop (Infinite)

loop {
    // body - use break to exit
    if done {
        break
    }
}

Return and Break

fn find_first_even(items: List[i32]) -> Option[i32] {
    for item in items {
        if item % 2 == 0 {
            return Some(item)
        }
    }
    return None
}
  • return exits the function with a value (or return for void functions)
  • break exits the innermost loop