Loops in Zig
While Loop
Used when the number of iterations is unknown.
zig var i: usize = 0; while (i < 5) : (i += 1) { std.debug.print("{d} ", .{i}); }
For Loop
Used to iterate over arrays or slices.
zig const items = [_]i32{ 1, 2, 3, 4, 5 }; for (items) |item| { std.debug.print("{d} ", .{item}); }
// With index for (items, 0..) |item, index| { std.debug.print("[{d}] = {d}\n", .{index, item}); }