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.
צבירה וספירה
- צובר (accumulator) הוא משתנה שיוצר תוצאה הולכת וגוברת לאורך הלולאה, כמו ב-
totalלמעלה. - מוניטור (counter) מוסיף
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. · לחץ על הרץ כדי לראות את התוצא כאן.