0PricingLogin
Zig Academy · Lesson

for Loops over Ranges and Items

Iterate sequences and index pairs.

Iterate with for

A for loop walks through the elements of a slice or array, handing you each item in turn. No manual index needed. 🚶

for (names) |name| {
    std.debug.print("{s}\n", .{name});
}

The Capture in Pipes

The name between pipes is the capture. On each pass it binds to the current element, giving you a clean, readable loop body.

All lessons in this course

  1. if as a Statement and Expression
  2. while Loops and continue Expressions
  3. for Loops over Ranges and Items
  4. break, continue, and Labeled Loops
← Back to Zig Academy