Loops: while, for, and accumulation · الحلقات: while، for، والتراكم
Repeating work
- A loop runs the same block of code many times, so you don't copy it by hand.
- C has three loops:
while,for, anddo-while. They all repeat while a condition is true. - The condition is checked each time around. When it becomes false, the loop stops.
تكرار المهام
- الحلقة تُنفذ نفس كتلة البرمجة مرات عديدة، لتجنب نسخها يدوياً.
- يحتوي C على ثلاث حلقات:
while،for، وdo-while. جميعها تتكرر طالما أن الشرط صحيح. - يتم فحص الشرط في كل دورة. وعندما يصبح غير صحيح، تتوقف الحلقة.
while loops
while (test) { ... }checks the test first, then runs the block if it is true.- Something inside the block must eventually make the test false, or the loop never ends.
- Use
whilewhen you don't know in advance how many times you will repeat.
حلقات while
while (test) { ... }يفحص الاختبار أولاً، ثم ينفذ الكتلة إذا كان صحيحاً.- يجب أن يقوم شيء داخل الكتلة بجعل الاختبار غير صحيح في النهاية، وإلا لن تنتهي الحلقة أبداً.
- استخدم
whileعندما لا تعرف مسبقاً عدد المرات التي ستكرر فيها.
for loops
- A
forloop puts three parts in one line:for (start; test; step). for (int i = 0; i < 5; i++)means: startiat0, keep going whilei < 5, add1each time.- Use
forwhen you are counting a known number of times.
حلقات for
- تضع حلقة
forثلاثة أجزاء في سطر واحد:for (start; test; step). for (int i = 0; i < 5; i++)تعني: ابدأiمن0، استمر طالماi < 5، أضف1في كل مرة.- استخدم
forعندما تكون تعدد عدداً معروفاً من المرات.
#include <stdio.h>
int main(void) {
int total = 0;
for (int i = 1; i <= 4; i++) {
total = total + i; // accumulate: 1, then 3, then 6, then 10
}
printf("%d\n", total); // 10
return 0;
}
Accumulation and counting
- An accumulator is a variable that builds up a result across the loop, like
totalabove. - A counter adds
1each time something happens, to count matches. break;stops the loop early;continue;skips to the next round.
التجميع والعد
- المُجمِّع متغير يجمع نتيجة عبر الحلقة، مثل
totalأعلاه. - العَدَّاد يُضيف
1في كل مرة يحدث فيها شيء ما، لعد التطابقات. break;يُوقف الحلقة مبكراً؛continue;يتخطى إلى الجولة التالية.
Common mistakes
- Do not put a
;right afterfor(...)— it makes an empty loop. - Change the condition so the loop can end.
أخطاء شائعة
- لا تضع
;مباشرة بعدfor(...)— لأن ذلك يُنشئ حلقة فارغة. - غيّر الشرط بحيث يمكن للحلقة أن تنتهي.
Now you try
- Set up your accumulator or counter before the loop, update it inside, and use it after.
- For the function tasks, do not write a
main— the checker provides one.
الآن جرب بنفسك
- عرّف مُجمِّعك أو عَدَّادك قبل الحلقة، حدّثه داخل الحلقة، واستخدمه بعد الحلقة.
- بالنسبة لمهام الدوال، لا تكتب
main— يوفره الفاحض لك.
Loops and accumulation · الحلقات التراكم
A loop repeats; a running total builds up each pass. · الحلقة تتكرر؛ المجموع المتجمع يتكون في كل دورة.
In main, use a for loop to print the numbers 1 to 5, each on its own line. · في main، استخدم حلقة for لطباعة الأعداد من 1 إلى 5، كل رقم في سطر مستخدم.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Complete int sum_to(int n) so it returns 1 + 2 + ... + n. Return 0 when n is 0 or negative. Do not write a main. · أكمل int sum_to(int n) لتُرجع 1 + 2 + ... + n. ارجع 0 عندما يكون n مساويًا لـ 0 أو سالبًا. لا تكتب main.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Complete int count_multiples(int n, int k) so it returns how many numbers from 1 to n are divisible by k. Use % inside a loop. Do not write a main. · أكمل int count_multiples(int n, int k) لتُرجع عدد الأعداد من 1 إلى n التي يقبل القسمة على k. استخدم % داخل حلقة. لا تكتب main.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.