Control Flow

if expressions

Sway supports if, else, and else if expressions that allow you to branch your code depending on conditions.

For example:

fn main() {
    let number = 6;

    if number % 4 == 0 {
        // do something
    } else if number % 3 == 0 {
        // do something else
    } else {
        // do something else
    }
}

Using if in a let statement

Like Rust, ifs are expressions in Sway. What this means is you can use if expressions on the right side of a let statement to assign the outcome to a variable.

let my_data = if some_bool < 10 { foo() } else { bar() };

Note that all branches of the if expression must return a value of the same type.

match expressions

Sway supports advanced pattern matching through exhaustive match expressions.

script;

fn foo() {
    // do something
}
fn bar() {
    // do something
}

enum SomeEnum {
    A: u64,
    B: bool,
    C: b256,
}

fn main() -> u64 {
    let x = 5;

    // Match as an expression.
    let a = match 8 {
        7 => {
            4
        },
        9 => {
            5
        },
        8 => {
            6
        },
        _ => {
            100
        },
    };

    // Match as a statement for control flow.
    match x {
        5 => {
            foo()
        },
        _ => {
            bar()
        },
    };

    // Match an enum
    let e = SomeEnum::A(42);
    let v = match e {
        SomeEnum::A(val) => {
            val
        },
        SomeEnum::B(true) => {
            1
        },
        SomeEnum::B(false) => {
            0
        },
        _ => {
            0
        },
    };

    // Match as expression used for a return.
    match 42 {
        0 => {
            24
        },
        foo => {
            foo
        },
    }
}

In the example above, braces around the code block following => in each match arm are not required unless the code block contains multiple statements. They are added in this example due to an issue in the Sway formatter.

Loops

while

Loops in Sway are currently limited to while loops. This is what they look like:

while counter < 10 {
    counter = counter + 1;
}

You need the while keyword, some condition (value < 10 in this case) which will be evaluated each iteration, and a block of code inside the curly braces ({...}) to execute each iteration.

break and continue

There are no break or continue keywords yet, but they're coming.

For now, the way to break out of a while loop early is to manually invalidate the condition. In this case, that just means setting counter to be >= 10.

Building on the previous example, here's what that might look like:

let mut counter = 0;
let mut break_early = false;
while counter < 10 {
    if break_early == true {
        // here we ensure the condition will evaluate to false, breaking the loop
        counter = 10
    } else {
        // calling some other function to set the bool value
        break_early = get_bool_value();
        counter = counter + 1;
    }
}

Nested loops

You can also use nested while loops if needed:

while condition_1 == true {
    // do stuff...
    while condition_2 == true {
        // do more stuff...
    }
}