while

A while loop is used to repeatedly execute an expression until some condition is no longer true.

while.zig

  1. const expect = @import("std").testing.expect;
  2. test "while basic" {
  3. var i: usize = 0;
  4. while (i < 10) {
  5. i += 1;
  6. }
  7. try expect(i == 10);
  8. }
  1. $ zig test while.zig
  2. Test [1/1] test "while basic"...
  3. All 1 tests passed.

Use break to exit a while loop early.

while.zig

  1. const expect = @import("std").testing.expect;
  2. test "while break" {
  3. var i: usize = 0;
  4. while (true) {
  5. if (i == 10)
  6. break;
  7. i += 1;
  8. }
  9. try expect(i == 10);
  10. }
  1. $ zig test while.zig
  2. Test [1/1] test "while break"...
  3. All 1 tests passed.

Use continue to jump back to the beginning of the loop.

while.zig

  1. const expect = @import("std").testing.expect;
  2. test "while continue" {
  3. var i: usize = 0;
  4. while (true) {
  5. i += 1;
  6. if (i < 10)
  7. continue;
  8. break;
  9. }
  10. try expect(i == 10);
  11. }
  1. $ zig test while.zig
  2. Test [1/1] test "while continue"...
  3. All 1 tests passed.

While loops support a continue expression which is executed when the loop is continued. The continue keyword respects this expression.

while.zig

  1. const expect = @import("std").testing.expect;
  2. test "while loop continue expression" {
  3. var i: usize = 0;
  4. while (i < 10) : (i += 1) {}
  5. try expect(i == 10);
  6. }
  7. test "while loop continue expression, more complicated" {
  8. var i: usize = 1;
  9. var j: usize = 1;
  10. while (i * j < 2000) : ({ i *= 2; j *= 3; }) {
  11. const my_ij = i * j;
  12. try expect(my_ij < 2000);
  13. }
  14. }
  1. $ zig test while.zig
  2. Test [1/2] test "while loop continue expression"...
  3. Test [2/2] test "while loop continue expression, more complicated"...
  4. All 2 tests passed.

While loops are expressions. The result of the expression is the result of the else clause of a while loop, which is executed when the condition of the while loop is tested as false.

break, like return, accepts a value parameter. This is the result of the while expression. When you break from a while loop, the else branch is not evaluated.

while.zig

  1. const expect = @import("std").testing.expect;
  2. test "while else" {
  3. try expect(rangeHasNumber(0, 10, 5));
  4. try expect(!rangeHasNumber(0, 10, 15));
  5. }
  6. fn rangeHasNumber(begin: usize, end: usize, number: usize) bool {
  7. var i = begin;
  8. return while (i < end) : (i += 1) {
  9. if (i == number) {
  10. break true;
  11. }
  12. } else false;
  13. }
  1. $ zig test while.zig
  2. Test [1/1] test "while else"...
  3. All 1 tests passed.

Labeled while

When a while loop is labeled, it can be referenced from a break or continue from within a nested loop:

test.zig

  1. test "nested break" {
  2. outer: while (true) {
  3. while (true) {
  4. break :outer;
  5. }
  6. }
  7. }
  8. test "nested continue" {
  9. var i: usize = 0;
  10. outer: while (i < 10) : (i += 1) {
  11. while (true) {
  12. continue :outer;
  13. }
  14. }
  15. }
  1. $ zig test test.zig
  2. Test [1/2] test "nested break"...
  3. Test [2/2] test "nested continue"...
  4. All 2 tests passed.

while with Optionals

Just like if expressions, while loops can take an optional as the condition and capture the payload. When null is encountered the loop exits.

When the |x| syntax is present on a while expression, the while condition must have an Optional Type.

The else branch is allowed on optional iteration. In this case, it will be executed on the first null value encountered.

while.zig

  1. const expect = @import("std").testing.expect;
  2. test "while null capture" {
  3. var sum1: u32 = 0;
  4. numbers_left = 3;
  5. while (eventuallyNullSequence()) |value| {
  6. sum1 += value;
  7. }
  8. try expect(sum1 == 3);
  9. var sum2: u32 = 0;
  10. numbers_left = 3;
  11. while (eventuallyNullSequence()) |value| {
  12. sum2 += value;
  13. } else {
  14. try expect(sum2 == 3);
  15. }
  16. }
  17. var numbers_left: u32 = undefined;
  18. fn eventuallyNullSequence() ?u32 {
  19. return if (numbers_left == 0) null else blk: {
  20. numbers_left -= 1;
  21. break :blk numbers_left;
  22. };
  23. }
  1. $ zig test while.zig
  2. Test [1/1] test "while null capture"...
  3. All 1 tests passed.

while with Error Unions

Just like if expressions, while loops can take an error union as the condition and capture the payload or the error code. When the condition results in an error code the else branch is evaluated and the loop is finished.

When the else |x| syntax is present on a while expression, the while condition must have an Error Union Type.

while.zig

  1. const expect = @import("std").testing.expect;
  2. test "while error union capture" {
  3. var sum1: u32 = 0;
  4. numbers_left = 3;
  5. while (eventuallyErrorSequence()) |value| {
  6. sum1 += value;
  7. } else |err| {
  8. try expect(err == error.ReachedZero);
  9. }
  10. }
  11. var numbers_left: u32 = undefined;
  12. fn eventuallyErrorSequence() anyerror!u32 {
  13. return if (numbers_left == 0) error.ReachedZero else blk: {
  14. numbers_left -= 1;
  15. break :blk numbers_left;
  16. };
  17. }
  1. $ zig test while.zig
  2. Test [1/1] test "while error union capture"...
  3. All 1 tests passed.

inline while

While loops can be inlined. This causes the loop to be unrolled, which allows the code to do some things which only work at compile time, such as use types as first class values.

test.zig

  1. const expect = @import("std").testing.expect;
  2. test "inline while loop" {
  3. comptime var i = 0;
  4. var sum: usize = 0;
  5. inline while (i < 3) : (i += 1) {
  6. const T = switch (i) {
  7. 0 => f32,
  8. 1 => i8,
  9. 2 => bool,
  10. else => unreachable,
  11. };
  12. sum += typeNameLength(T);
  13. }
  14. try expect(sum == 9);
  15. }
  16. fn typeNameLength(comptime T: type) usize {
  17. return @typeName(T).len;
  18. }
  1. $ zig test test.zig
  2. Test [1/1] test "inline while loop"...
  3. All 1 tests passed.

It is recommended to use inline loops only for one of these reasons:

  • You need the loop to execute at comptime for the semantics to work.
  • You have a benchmark to prove that forcibly unrolling the loop in this way is measurably faster.

See also: