for

for.zig

  1. constassert=@import("std").debug.assert;
  2. test "for basics"{
  3. const items =[_]i32 {4,5,3,4,0};
  4. var sum: i32 =0;
  5. // For loops iterate over slices and arrays.
  6. for(items)|value|{
  7. // Break and continue are supported.
  8. if(value ==0){
  9. continue;
  10. }
  11. sum += value;
  12. }
  13. assert(sum ==16);
  14. // To iterate over a portion of a slice, reslice.
  15. for(items[0..1])|value|{
  16. sum += value;
  17. }
  18. assert(sum ==20);
  19. // To access the index of iteration, specify a second capture value.
  20. // This is zero-indexed.
  21. var sum2: i32 =0;
  22. for(items)|value, i|{
  23. assert(@TypeOf(i)== usize);
  24. sum2 +=@intCast(i32, i);
  25. }
  26. assert(sum2 ==10);
  27. }
  28. test "for reference"{
  29. var items =[_]i32 {3,4,2};
  30. // Iterate over the slice by reference by
  31. // specifying that the capture value is a pointer.
  32. for(items)|*value|{
  33. value.*+=1;
  34. }
  35. assert(items[0]==4);
  36. assert(items[1]==5);
  37. assert(items[2]==3);
  38. }
  39. test "for else"{
  40. // For allows an else attached to it, the same as a while loop.
  41. var items =[_]?i32 {3,4,null,5};
  42. // For loops can also be used as expressions.
  43. // Similar to while loops, when you break from a for loop, the else branch is not evaluated.
  44. var sum: i32 =0;
  45. const result =for(items)|value|{
  46. if(value !=null){
  47. sum += value.?;
  48. }
  49. }else blk:{
  50. assert(sum ==12);
  51. break:blk sum;
  52. };
  53. assert(result ==12);
  54. }
  1. $ zig test for.zig
  2. 1/3 test "for basics"...OK
  3. 2/3 test "for reference"...OK
  4. 3/3 test "for else"...OK
  5. All3 tests passed.

Labeled for

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

test.zig

  1. const std =@import("std");
  2. constassert= std.debug.assert;
  3. test "nested break"{
  4. var count: usize =0;
  5. outer:for([_]i32{1,2,3,4,5})|_|{
  6. for([_]i32{1,2,3,4,5})|_|{
  7. count +=1;
  8. break:outer;
  9. }
  10. }
  11. assert(count ==1);
  12. }
  13. test "nested continue"{
  14. var count: usize =0;
  15. outer:for([_]i32{1,2,3,4,5,6,7,8})|_|{
  16. for([_]i32{1,2,3,4,5})|_|{
  17. count +=1;
  18. continue:outer;
  19. }
  20. }
  21. assert(count ==8);
  22. }
  1. $ zig test test.zig
  2. 1/2 test "nested break"...OK
  3. 2/2 test "nested continue"...OK
  4. All2 tests passed.

inline for

For 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. The capture value and iterator value of inlined for loops are compile-time known.

test.zig

  1. constassert=@import("std").debug.assert;
  2. test "inline for loop"{
  3. const nums =[_]i32{2,4,6};
  4. var sum: usize =0;
  5. inlinefor(nums)|i|{
  6. const T =switch(i){
  7. 2=> f32,
  8. 4=> i8,
  9. 6=>bool,
  10. else=> unreachable,
  11. };
  12. sum += typeNameLength(T);
  13. }
  14. assert(sum ==9);
  15. }
  16. fn typeNameLength(comptime T: type) usize {
  17. return@typeName(T).len;
  18. }
  1. $ zig test test.zig
  2. 1/1 test "inline for loop"...OK
  3. All1 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: