authorgravatar for ntrel@noreply.codeberg.orgntrel <ntrel@noreply.codeberg.org> 2026-10-11 19:02:13+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-10-11 19:02:13+02:00
log6b5a35e4ff3d2a18eb21be4656eeaa6812e223f1
tree7d204e863148c6b27f8350476da03a9cdfd69512
parent9276a56e3cee12a067f93d719a5e7f2762dd4603

Update doc/langref/test_for.zig (#37181)

Use -1 element instead of 0, as 0 wouldn't affect sum. Fix 'reslice' wording as example is an array, not a slice. Increase slice length to 2 to demonstrate feature better. Split out "for ranges" test from basics with shorter array. Use usize sum to avoid cast. Improve comments. Reuse sum vars. Reviewed-on: https://codeberg.org/ziglang/zig/pulls/37181 Reviewed-by: Andrew Kelley <andrew@ziglang.org>

1 files changed, 24 insertions(+), 18 deletions(-)

doc/langref/test_for.zig+24-18
...@@ -1,41 +1,47 @@...@@ -1,41 +1,47 @@
1const expectEqual = @import("std").testing.expectEqual;1const expectEqual = @import("std").testing.expectEqual;
22
3test "for basics" {3test "for basics" {
4 const items = [_]i32{ 4, 5, 3, 4, 0 };4 const items = [_]i32{ 4, 5, 3, -1, 4 };
5 var sum: i32 = 0;5 var sum: i32 = 0;
66
7 // For loops iterate over slices and arrays.7 // For loops iterate over slices and arrays.
8 for (items) |value| {8 for (items) |value| {
9 // Break and continue are supported.9 // Break and continue are supported.
10 if (value == 0) {10 if (value < 0) {
11 continue;11 continue;
12 }12 }
13 sum += value;13 sum += value;
14 }14 }
15 try expectEqual(16, sum);15 try expectEqual(16, sum);
1616
17 // To iterate over a portion of a slice, reslice.17 // Use slicing to iterate over a portion of a slice or array.
18 for (items[0..1]) |value| {18 sum = 0;
19 for (items[0..2]) |value| {
19 sum += value;20 sum += value;
20 }21 }
21 try expectEqual(20, sum);22 try expectEqual(9, sum);
23}
24
25test "for ranges" {
26 const items = [_]u8{1, 2, 3};
27 var sum: usize = 0;
2228
23 // To access the index of iteration, specify a second condition as well29 // To access the index of iteration, specify an unbounded range as the
24 // as a second capture value.30 // second condition, as well as a second capture value.
25 var sum2: i32 = 0;31 for (items, 0..) |e, i| {
26 for (items, 0..) |_, i| {
27 try expectEqual(usize, @TypeOf(i));32 try expectEqual(usize, @TypeOf(i));
28 sum2 += @as(i32, @intCast(i));33 sum += i;
34 try expectEqual(e, i + 1);
29 }35 }
30 try expectEqual(10, sum2);36 try expectEqual(3, sum); // 0 + 1 + 2
3137
32 // To iterate over consecutive integers, use the range syntax.38 // To iterate over consecutive integers, use bounded range syntax.
33 // Unbounded range is always a compile error.39 // A lone unbounded range is a compile error.
34 var sum3: usize = 0;40 sum = 0;
35 for (0..5) |i| {41 for (0..5) |i| {
36 sum3 += i;42 sum += i;
37 }43 }
38 try expectEqual(10, sum3);44 try expectEqual(10, sum);
39}45}
4046
41test "multi object for" {47test "multi object for" {
...@@ -44,8 +50,8 @@ test "multi object for" {...@@ -44,8 +50,8 @@ test "multi object for" {
44 var count: usize = 0;50 var count: usize = 0;
4551
46 // Iterate over multiple objects.52 // Iterate over multiple objects.
47 // All lengths must be equal at the start of the loop, otherwise detectable53 // All bounded objects must have equal lengths.
48 // illegal behavior occurs.54 // There must be at least one bounded object.
49 for (items, items2) |i, j| {55 for (items, items2) |i, j| {
50 count += i + j;56 count += i + j;
51 }57 }