חלקPart 1: יסודותFoundations
כ-3 שעות (כולל הפסקה)About 3 hours (with a break)
לולאותLoops
for, while, do-while ו-for-each, break ו-continue, לולאות מקוננות ומעקב אחרי משתני לולאה.
for, while, do-while and for-each, break and continue, nested loops and tracing loop variables.
הנקודות החשובותKey points
- לולאה: קוד שחוזר שוב ושוב:loop: Code that repeats:
- סיבוב (איטרציה): פעם אחת שבה גוף הלולאה רץ.iteration: One time the loop body runs.
- שלושה חלקים בסוגריים: התחלה (
int i = 1), תנאי להמשך (i <= 3), ושינוי בסוף כל סיבוב (i++).Three parts in the brackets: a start (int i = 1), a condition to keep going (i <= 3), and a change at the end of each round (i++). - צפו בסימולציה איך
iעולה, ואיך שורת ה-for נבדקת שוב בכל סיבוב.Watch in the simulation howigoes up, and how the for line is checked again each round. - צובר מתחיל לפני הלולאה, ובכל סיבוב מוסיפים לו.An accumulator starts before the loop, and each round adds to it.
- בסוף הוא מחזיק את התוצאה.At the end it holds the result.
whileבודק לפני כל סיבוב, ומתאים כשלא יודעים כמה סיבובים יהיו.whilechecks before every round, and suits cases where you don't know how many rounds there'll be.do-whileבודק בסוף, ולכן רץ לפחות פעם אחת.do-whilechecks at the end, so it runs at least once.- for-each עובר על כל פריט.for-each goes over every item.
מונחיםTerms
- לולאהloop: קוד שחוזר שוב ושוב:
for,while,do-whileו-for-each.Code that repeats:for,while,do-whileand for-each. - סיבוב (איטרציה)iteration: פעם אחת שבה גוף הלולאה רץ.One time the loop body runs.
- מונהcounter: משתנה שסופר את הסיבובים, בדרך כלל
i: מתחיל, נבדק בתנאי, ועולה בסוף כל סיבוב.A variable that counts the rounds, usuallyi: it starts, is checked by the condition, and goes up at the end of each round. - צוברaccumulator: משתנה שאוסף תוצאה לאורך הלולאה, כמו
sum += i. מאתחלים אותו לפני הלולאה.A variable that gathers a result through the loop, likesum += i. Initialize it before the loop. - for-eachfor-each:
for (char c : s.toCharArray()): עובר על כל פריט, בלי מונה.for (char c : s.toCharArray()): goes over every item, with no counter. - לולאה מקוננתnested loop: לולאה בתוך לולאה: על כל סיבוב של החיצונית, הפנימית רצה מההתחלה עד הסוף.A loop inside a loop: for each round of the outer one, the inner one runs from start to finish.
- טעות של אחד (off-by-one)off-by-one: לולאה שרצה פעם אחת יותר מדי או פחות מדי, לרוב בגלל
<במקום<=או התחלה מ-1 במקום מ-0.A loop that runs one time too many or too few, usually from<instead of<=or starting at 1 instead of 0. - לולאה אינסופיתinfinite loop: לולאה שהתנאי שלה אף פעם לא נהיה false, למשל כששוכחים לשנות את המונה.A loop whose condition never becomes false, for example when you forget to change the counter.
- לולאהloop: קוד שחוזר שוב ושוב:
for,while,do-whileו-for-each.Code that repeats:for,while,do-whileand for-each. - סיבוב (איטרציה)iteration: פעם אחת שבה גוף הלולאה רץ.One time the loop body runs.
- מונהcounter: משתנה שסופר את הסיבובים, בדרך כלל
i: מתחיל, נבדק בתנאי, ועולה בסוף כל סיבוב.A variable that counts the rounds, usuallyi: it starts, is checked by the condition, and goes up at the end of each round. - צוברaccumulator: משתנה שאוסף תוצאה לאורך הלולאה, כמו
sum += i. מאתחלים אותו לפני הלולאה.A variable that gathers a result through the loop, likesum += i. Initialize it before the loop. - for-eachfor-each:
for (char c : s.toCharArray()): עובר על כל פריט, בלי מונה.for (char c : s.toCharArray()): goes over every item, with no counter. - לולאה מקוננתnested loop: לולאה בתוך לולאה: על כל סיבוב של החיצונית, הפנימית רצה מההתחלה עד הסוף.A loop inside a loop: for each round of the outer one, the inner one runs from start to finish.
- טעות של אחד (off-by-one)off-by-one: לולאה שרצה פעם אחת יותר מדי או פחות מדי, לרוב בגלל
<במקום<=או התחלה מ-1 במקום מ-0.A loop that runs one time too many or too few, usually from<instead of<=or starting at 1 instead of 0. - לולאה אינסופיתinfinite loop: לולאה שהתנאי שלה אף פעם לא נהיה false, למשל כששוכחים לשנות את המונה.A loop whose condition never becomes false, for example when you forget to change the counter.
Cheatsheet
לולאת forThe for loop
for (int i = 1; i <= 3; i++) {
System.out.println("סיבוב " + i);
}
System.out.println("סוף");for (int i = 1; i <= 3; i++) {
System.out.println("Round " + i);
}
System.out.println("Done");צובר: סכום בתוך לולאהAn accumulator: a sum in a loop
int sum = 0;
for (int i = 1; i <= 4; i++) {
sum += i;
}
System.out.println(sum);while ו-do-whilewhile and do-while
int n = 20;
int steps = 0;
while (n > 1) {
n = n / 2;
steps++;
}
System.out.println(steps);
int k = 10;
do {
System.out.println("פעם אחת לפחות");int n = 20;
int steps = 0;
while (n > 1) {
n = n / 2;
steps++;
}
System.out.println(steps);
int k = 10;
do {
System.out.println("at least once");for-each, break ו-continuefor-each, break and continue
for (char c : "ja-va!".toCharArray()) {
if (c == '-') {
continue;
}
if (c == '!') {
break;
}
System.out.print(c);
}
System.out.println();לולאות מקוננותNested loops
for (int row = 1; row <= 3; row++) {
for (int col = 1; col <= row; col++) {
System.out.print("*");
}
System.out.println();
}שגיאות שפגשנוErrors we met
| שגיאהError | מה זה אומרWhat it means | מה עושיםWhat to do |
|---|