Skip to main content
Version: Zig 0.12.0

While loops

Zig's while loop has three parts - a condition, a block and a continue expression.

Without a continue expression.

const expect = @import("std").testing.expect;

test "while" {
var i: u8 = 2;
while (i < 100) {
i *= 2;
}
try expect(i == 128);
}

With a continue expression.

const expect = @import("std").testing.expect;

test "while with continue expression" {
var sum: u8 = 0;
var i: u8 = 1;
while (i <= 10) : (i += 1) {
sum += i;
}
try expect(sum == 55);
}

With a continue.

const expect = @import("std").testing.expect;

test "while with continue" {
var sum: u8 = 0;
var i: u8 = 0;
while (i <= 3) : (i += 1) {
if (i == 2) continue;
sum += i;
}
try expect(sum == 4);
}

With a break.

const expect = @import("std").testing.expect;

test "while with break" {
var sum: u8 = 0;
var i: u8 = 0;
while (i <= 3) : (i += 1) {
if (i == 2) break;
sum += i;
}
try expect(sum == 1);
}