for Loops over Ranges and Items
Iterate sequences and index pairs.
for Loops over Ranges and Items is a free Zig Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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.
Capturing by Reference
Prefix the capture with * to get a pointer to each element. That lets you modify items in place rather than copying them.
for (scores) |*s| {
s.* += 1;
}Getting the Index
Add a second sequence to pair items with an index. Zig walks both together, giving you the value and its position.
for (items, 0..) |item, i| {
std.debug.print("{}: {}\n", .{ i, item });
}0.. Is an Index Sequence
The 0.. form produces indices that match the length of the items. It is not a separate array, just a counter walked in lockstep.
Looping a Fixed Count
To repeat a set number of times, iterate over a range like 0..count. The capture holds the current number on each pass.
for (0..3) |n| {
std.debug.print("step {}\n", .{n});
}Ranges Are Half-Open
A range like 0..5 includes 0 but stops before 5, giving exactly five values. This half-open style matches slice lengths perfectly.
Iterating Two Slices
Pass several slices to walk them in parallel. Zig requires them to share the same length, then yields one element from each per pass.
for (keys, values) |k, v| {
put(k, v);
}break and continue Apply
Inside a for loop you can use break to exit early and continue to skip to the next item, just like in a while loop.
for as an Expression
A for can yield a value too. break returns a result, while an else clause supplies the value if the loop completes without breaking.
const idx = for (items, 0..) |x, i| {
if (x == target) break i;
} else null;Pick for or while
Reach for for when you have a sequence to walk. Choose while when looping depends on a condition rather than a collection.
Quick Check
You need both each item and its index in one for loop. Which header does that?
Recap
You ran for loops: capturing items, by-reference edits, indices with 0.., counting ranges, parallel slices, and the value-returning form. 🎯
Frequently asked questions
Is the “for Loops over Ranges and Items” lesson free?
Yes — the full text of “for Loops over Ranges and Items” is free to read here on the web, and the Zig Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Zig Academy course, upgrade to CoddyKit PRO.
What will I learn in “for Loops over Ranges and Items”?
Iterate sequences and index pairs. You practise Zig Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Zig Academy?
No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “for Loops over Ranges and Items” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Zig Academy lesson?
Yes. Every Zig Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- if as a Statement and Expression
- while Loops and continue Expressions
- for Loops over Ranges and Items
- break, continue, and Labeled Loops