حلقات for على النطاقات والعناصر
كرّروا على التسلسلات وأزواج الفهارس
حلقات for على النطاقات والعناصر درس مجاني في Zig Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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/7) وفتح باقي دورة Zig Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Zig Academy 4 دروس في المجموع.
ماذا ستتعلم في «حلقات for على النطاقات والعناصر»؟
كرّروا على التسلسلات وأزواج الفهارس تتمرن على Zig Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Zig Academy؟
لا تُشترط خبرة سابقة. Zig Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «حلقات for على النطاقات والعناصر»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Zig Academy هذا؟
نعم. كل درس في Zig Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- if بوصفها تعليمة وتعبيراً
- حلقات while وتعبيرات continue
- حلقات for على النطاقات والعناصر
- break وcontinue والحلقات ذات التسميات