Skip to main content
Version: v0.35.0

Control Flow

If Expressions

Noir supports if-else statements. The syntax is most similar to Rust's where it is not required for the statement's conditional to be surrounded by parentheses.

let a = 0;
let mut x: u32 = 0;

if a == 0 {
if a != 0 {
x = 6;
} else {
x = 2;
}
} else {
x = 5;
assert(x == 5);
}
assert(x == 2);

Loops

Noir has one kind of loop: the for loop. for loops allow you to repeat a block of code multiple times.

The following block of code between the braces is run 10 times.

for i in 0..10 {
// do something
}

The index for loops is of type u64.

Break and Continue

In unconstrained code, break and continue are also allowed in for loops. These are only allowed in unconstrained code since normal constrained code requires that Noir knows exactly how many iterations a loop may have. break and continue can be used like so:

for i in 0 .. 10 {
println("Iteration start")

if i == 2 {
continue;
}

if i == 5 {
break;
}

println(i);
}
println("Loop end")

When used, break will end the current loop early and jump to the statement after the for loop. In the example above, the break will stop the loop and jump to the println("Loop end").

continue will stop the current iteration of the loop, and jump to the start of the next iteration. In the example above, continue will jump to println("Iteration start") when used. Note that the loop continues as normal after this. The iteration variable i is still increased by one as normal when continue is used.

break and continue cannot currently be used to jump out of more than a single loop at a time.