範囲と項目に対するforループ
シーケンスとインデックス・値のペアを反復処理します。
「範囲と項目に対するforループ」はCoddyKit上の無料Zig Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはZig Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Zig Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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. 🎯
よくある質問
「範囲と項目に対するforループ」レッスンは無料ですか?
はい。「範囲と項目に対するforループ」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Zig Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Zig Academyコースには全4レッスンが含まれています。
「範囲と項目に対するforループ」で何を学びますか?
シーケンスとインデックス・値のペアを反復処理します。 ブラウザで直接実行するハンズオンコードでZig Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Zig Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのZig Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「範囲と項目に対するforループ」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このZig Academyレッスンでコードを書いて実行できますか?
はい。すべてのZig Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 文と式としてのif
- whileループとcontinue式
- 範囲と項目に対するforループ
- break、continue、ラベル付きループ