BRAIDGROUP
RESEARCH & DEV
10. Documentation

Control Flow

Braid supports three main control flow constructs: if/else for conditional branching, while for looping, and match for pattern matching. These are compiled to branch instructions (OP_JUMP, OP_JUMP_IF_FALSE, OP_LOOP) in the bytecode VM.

If

The if statement evaluates a boolean condition and executes a block if the condition is true:

fn main() {
    let x = 5

    if x > 3 {
        print("x is greater than 3")
    }
}

If / Else

fn check(x: int) {
    if x > 10 {
        print("x > 10")
    } else {
        print("x <= 10")
    }
}

fn main() {
    check(5)    // x <= 10
    check(20)   // x > 10
}

If / Else If / Else

fn classify(x: int) {
    if x > 20 {
        print("x > 20")
    } else if x > 10 {
        print("10 < x <= 20")
    } else {
        print("x <= 10")
    }
}

fn main() {
    classify(5)     // x <= 10
    classify(15)    // 10 < x <= 20
    classify(25)    // x > 20
}

Nested If

fn check_range(x: int) {
    if x &gt;= 0 {
        if x < 10 {
            print("single digit")
        } else {
            print("multi-digit")
        }
    } else {
        print("negative")
    }
}

While Loops

The while statement repeatedly executes a block as long as the condition is true:

fn count_to(n: int) {
    let i = 0
    while i < n {
        print(i)
        i = i + 1
    }
}

fn main() {
    count_to(5)   // prints 0, 1, 2, 3, 4
}

Nested While

fn print_grid(rows: int, cols: int) {
    let i = 0
    while i < rows {
        let j = 0
        while j < cols {
            print("(" + i + "," + j + ")")
            j = j + 1
        }
        i = i + 1
    }
}

fn main() {
    print_grid(3, 2)
    // (0,0) (0,1) (1,0) (1,1) (2,0) (2,1)
}

While with Conditionals

fn find_first(values: [int], target: int) -> int {
    let i = 0
    while i < 100 {
        if values[i] == target {
            return i
        }
        i = i + 1
    }
    return -1
}

fn main() {
    let arr = [10, 20, 30, 40, 50]
    let idx = find_first(arr, 30)
    print(idx)    // 2
}

Match on Integers

fn http_status(code: int) {
    match code {
        200 => print("OK"),
        201 => print("Created"),
        301 => print("Moved Permanently"),
        400 => print("Bad Request"),
        401 => print("Unauthorized"),
        403 => print("Forbidden"),
        404 => print("Not Found"),
        500 => print("Internal Server Error"),
        502 => print("Bad Gateway"),
        503 => print("Service Unavailable"),
        _ => print("Unknown Status: " + code)
    }
}

Match on Strings

fn handle_command(cmd: string) {
    match cmd {
        "start" => print("Starting..."),
        "stop" => print("Stopping..."),
        "restart" => print("Restarting..."),
        "status" => print("Checking status..."),
        "help" => print("Available commands: start, stop, restart, status, help"),
        _ => print("Unknown command: " + cmd)
    }
}

Match on Booleans

fn respond(flag: bool) {
    match flag {
        true => print("It's true!"),
        false => print("It's false!")
    }
}

Match on Enums

enum Color {
    Red
    Green
    Blue
}

fn describe(c: Color) {
    match c {
        Color.Red => print("Red: the color of passion"),
        Color.Green => print("Green: the color of nature"),
        Color.Blue => print("Blue: the color of sky"),
        _ => print("Unknown color")
    }
}

Match with Block Bodies

fn process(x: int) {
    match x {
        0 => {
            print("x is zero")
            print("no work to do")
        },
        1 => {
            print("x is one")
            print("starting process")
        },
        _ => {
            print("x is " + x)
            print("processing...")
            let result = x * 2
            print("result: " + result)
        }
    }
}

Match on Nil

fn check_result(val: int) {
    match val {
        nil => print("Operation failed"),
        _ => print("Result: " + val)
    }
}

Return

fn abs(x: int) -> int {
    if x < 0 {
        return -x
    }
    return x
}

fn factorial(n: int) -> int {
    if n <= 1 {
        return 1
    }
    return n * factorial(n - 1)
}

fn is_even(n: int) -> bool {
    return n % 2 == 0
}

Combined Example

fn fizzbuzz(n: int) {
    let i = 1
    while i <= n {
        if i % 15 == 0 {
            print("FizzBuzz")
        } else if i % 3 == 0 {
            print("Fizz")
        } else if i % 5 == 0 {
            print("Buzz")
        } else {
            print(i)
        }
        i = i + 1
    }
}

fn main() {
    fizzbuzz(15)
}