Control Flow

RUDRA provides conditional branching and loops with a familiar shape. The samples below are complete programs: print takes only a string and adds no newline, and the exit status is the value main returns.

Conditionals

This program prints medium.

fn classify(n: int) -> str {
    if n > 10 {
        return "big";
    } else if n > 5 {
        return "medium";
    } else {
        return "small";
    }
}
fn main() -> int {
    print(classify(7) + "
");
    return 0;
}
return main();

While loops

fn main() -> int {
    let mut i = 0;
    let mut total = 0;
    while i < 5 {
        total = total + i;
        i = i + 1;
    }
    print(to_str(total) + "
");
    return 0;
}
return main();

This program prints 10.

For loops over a range

fn main() -> int {
    for k in 0..3 {
        print(to_str(k) + "
");
    }
    let xs = list_new();
    push(xs, 4);
    push(xs, 8);
    for v in xs {
        if v == 4 {
            continue;
        }
        print(to_str(v) + "
");
    }
    return 0;
}
return main();

The range 0..3 yields 0, 1 and 2. for v in list iterates a list[int]; break and continue work in all loops. The program above prints 0, 1, 2 and 8, one per line.

Operators

Arithmetic: + - * / %. Comparison: == != < > <= >=. Logical: && || !.