מפגש 5: for, while, do-while ו-for-each, break ו-continue, לולאות מקוננות ומעקב אחרי משתני לולאה.Session 5: for, while, do-while and for-each, break and continue, nested loops and tracing loop variables.
for, while, do-while ו-for-each, break ו-continue, לולאות מקוננות ומעקב אחרי משתני לולאה. בסוף המפגש מוסיפים צעד לפרויקט המלווה.for, while, do-while and for-each, break and continue, nested loops and tracing loop variables. At the end we add a step to the companion project.
מה נעשה היוםToday's plan
מונחיםTerms
לולאת forThe for loop
צובר: סכום בתוך לולאהAn accumulator: a sum in a loop
while ו-do-whilewhile and do-while
for-each, break ו-continuefor-each, break and continue
לולאות מקוננותNested loops
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.
נושאTopic 1
מונחיםTerms
המונחים של המפגשThis session's terms
לולאה: קוד שחוזר שוב ושוב:loop: Code that repeats:
סיבוב (איטרציה): פעם אחת שבה גוף הלולאה רץ.iteration: One time the loop body runs.
מונה: משתנה שסופר את הסיבובים, בדרך כלל i:counter: A variable that counts the rounds, usually i:
צובר: משתנה שאוסף תוצאה לאורך הלולאה, כמו sum += i.accumulator: A variable that gathers a result through the loop, like sum += i.
for-each: `for (char c :for-each: `for (char c :
לולאה מקוננת: לולאה בתוך לולאה:nested loop: A loop inside a loop:
לפני הקוד, המילים. כל מונח כאן מקושר גם במילון של הקורס.Before the code, the words. Every term here is also linked in the course glossary.
נושאTopic 2
לולאת forThe for loop
לולאת forThe for loop
שלושה חלקים בסוגריים: התחלה (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 how i goes up, and how the for line is checked again each round.
שלושה חלקים בסוגריים: התחלה (int i = 1), תנאי להמשך (i <= 3), ושינוי בסוף כל סיבוב (i++). צפו בסימולציה איך i עולה, ואיך שורת ה-for נבדקת שוב בכל סיבוב.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++). Watch in the simulation how i goes up, and how the for line is checked again each round.
שאלה לכיתהQuestion for the class
מה ידפיס הקוד?What will this print?
main
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");
נותנים לכיתה חצי דקה לנחש, ואז עוברים לשקופית הבאה. (זה הגוף של main.)Give the class half a minute to guess, then move on. (This is the body of main.)
לולאת forThe for loop
Main.java · main
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");
פלטOutput
סיבוב 1
סיבוב 2
סיבוב 3
סוף
Round 1
Round 2
Round 3
Done
מריצים ומשווים לניחושים.Run it and compare with the guesses.
נושאTopic 3
צובר: סכום בתוך לולאהAn accumulator: a sum in a loop
צובר: סכום בתוך לולאהAn accumulator: a sum in a loop
צובר מתחיל לפני הלולאה, ובכל סיבוב מוסיפים לו.An accumulator starts before the loop, and each round adds to it.
בסוף הוא מחזיק את התוצאה.At the end it holds the result.
צובר מתחיל לפני הלולאה, ובכל סיבוב מוסיפים לו. בסוף הוא מחזיק את התוצאה.An accumulator starts before the loop, and each round adds to it. At the end it holds the result.
שאלה לכיתהQuestion for the class
מה ידפיס הקוד?What will this print?
main
int sum =0;
for (int i =1; i <=4; i++) {
sum += i;
}
System.out.println(sum);
נותנים לכיתה חצי דקה לנחש, ואז עוברים לשקופית הבאה. (זה הגוף של main.)Give the class half a minute to guess, then move on. (This is the body of main.)
צובר: סכום בתוך לולאהAn accumulator: a sum in a loop
Main.java · main
int sum =0;
for (int i =1; i <=4; i++) {
sum += i;
}
System.out.println(sum);
פלטOutput
10
מריצים ומשווים לניחושים.Run it and compare with the guesses.
נושאTopic 4
while ו-do-whilewhile and do-while
while ו-do-whilewhile and do-while
while בודק לפני כל סיבוב, ומתאים כשלא יודעים כמה סיבובים יהיו.while checks before every round, and suits cases where you don't know how many rounds there'll be.
do-while בודק בסוף, ולכן רץ לפחות פעם אחת.do-while checks at the end, so it runs at least once.
שכחתם לשנות את המשתנה?Forgot to change the variable?
while בודק לפני כל סיבוב, ומתאים כשלא יודעים כמה סיבובים יהיו. do-while בודק בסוף, ולכן רץ לפחות פעם אחת. שכחתם לשנות את המשתנה? זו לולאה אינסופית.while checks before every round, and suits cases where you don't know how many rounds there'll be. do-while checks at the end, so it runs at least once. Forgot to change the variable? That's an infinite loop.
נושאTopic 5
for-each, break ו-continuefor-each, break and continue
for-each, break ו-continuefor-each, break and continue
continue מדלג לסיבוב הבא, ו-break יוצא מהלולאה מיד.continue skips to the next round, and break leaves the loop at once.
for-each עובר על כל פריט. continue מדלג לסיבוב הבא, ו-break יוצא מהלולאה מיד.for-each goes over every item. continue skips to the next round, and break leaves the loop at once.
שאלה לכיתהQuestion for the class
מה ידפיס הקוד?What will this print?
main
for (char c :"ja-va!".toCharArray()) {
if (c =='-') {
continue;
}
if (c =='!') {
break;
}
System.out.print(c);
}
System.out.println();
נותנים לכיתה חצי דקה לנחש, ואז עוברים לשקופית הבאה. (זה הגוף של main.)Give the class half a minute to guess, then move on. (This is the body of main.)
for-each, break ו-continuefor-each, break and continue
Main.java · main
for (char c :"ja-va!".toCharArray()) {
if (c =='-') {
continue;
}
if (c =='!') {
break;
}
System.out.print(c);
}
System.out.println();
פלטOutput
java
מריצים ומשווים לניחושים.Run it and compare with the guesses.
נושאTopic 6
לולאות מקוננותNested loops
לולאות מקוננותNested loops
על כל סיבוב של הלולאה החיצונית, הפנימית רצה מההתחלה.For every round of the outer loop, the inner one runs from the start.
כך בונים טבלאות וצורות.That's how tables and shapes are built.
על כל סיבוב של הלולאה החיצונית, הפנימית רצה מההתחלה. כך בונים טבלאות וצורות.For every round of the outer loop, the inner one runs from the start. That's how tables and shapes are built.
שאלה לכיתהQuestion for the class
מה ידפיס הקוד?What will this print?
main
for (int row =1; row <=3; row++) {
for (int col =1; col <= row; col++) {
System.out.print("*");
}
System.out.println();
}
נותנים לכיתה חצי דקה לנחש, ואז עוברים לשקופית הבאה. (זה הגוף של main.)Give the class half a minute to guess, then move on. (This is the body of main.)
לולאות מקוננותNested loops
Main.java · main
for (int row =1; row <=3; row++) {
for (int col =1; col <= row; col++) {
System.out.print("*");
}
System.out.println();
}
פלטOutput
*
**
***
מריצים ומשווים לניחושים.Run it and compare with the guesses.
נושאTopic 7
פרויקט מדףShelf project
הצעד של היום בפרויקט מדףToday's step in the Shelf project
מדף, צעד 5: רשימה ממוספרתShelf, step 5: a numbered list
במחרוזת books יש שמות של 3 ספרים מופרדים ב-;: "Dune;Emma;Ulysses". עברו עליהם (books.split(";") נותן אותם אחד-אחד; מערכים נלמד במפגש 8) והדפיסו רשימה ממוספרת 1. Dune וכן הלאה, ובסוף 3 books.books holds 3 titles separated by ;: "Dune;Emma;Ulysses". Go over them (books.split(";") hands them out one by one; arrays come in session 8) and print a numbered list 1. Dune and so on, then 3 books at the end.
לולאה: קוד שחוזר שוב ושוב:loop: Code that repeats:
המונחים של המפגשThis session's terms
סיבוב (איטרציה): פעם אחת שבה גוף הלולאה רץ.iteration: One time the loop body runs.
המונחים של המפגשThis session's terms
מונה: משתנה שסופר את הסיבובים, בדרך כלל i:counter: A variable that counts the rounds, usually i:
המונחים של המפגשThis session's terms
צובר: משתנה שאוסף תוצאה לאורך הלולאה, כמו sum += i.accumulator: A variable that gathers a result through the loop, like sum += i.
המונחים של המפגשThis session's terms
for-each: `for (char c :for-each: `for (char c :
המונחים של המפגשThis session's terms
לולאה מקוננת: לולאה בתוך לולאה:nested loop: A loop inside a loop:
המונחים של המפגשThis session's terms
שלושה חלקים בסוגריים: התחלה (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++).
לולאת forThe for loop
צפו בסימולציה איך i עולה, ואיך שורת ה-for נבדקת שוב בכל סיבוב.Watch in the simulation how i goes up, and how the for line is checked again each round.
לולאת forThe for loop
צובר מתחיל לפני הלולאה, ובכל סיבוב מוסיפים לו.An accumulator starts before the loop, and each round adds to it.
צובר: סכום בתוך לולאהAn accumulator: a sum in a loop
בסוף הוא מחזיק את התוצאה.At the end it holds the result.
צובר: סכום בתוך לולאהAn accumulator: a sum in a loop
while בודק לפני כל סיבוב, ומתאים כשלא יודעים כמה סיבובים יהיו.while checks before every round, and suits cases where you don't know how many rounds there'll be.
while ו-do-whilewhile and do-while
do-while בודק בסוף, ולכן רץ לפחות פעם אחת.do-while checks at the end, so it runs at least once.
while ו-do-whilewhile and do-while
שכחתם לשנות את המשתנה?Forgot to change the variable?
for-each, break ו-continuefor-each, break and continue
continue מדלג לסיבוב הבא, ו-break יוצא מהלולאה מיד.continue skips to the next round, and break leaves the loop at once.
for-each, break ו-continuefor-each, break and continue
על כל סיבוב של הלולאה החיצונית, הפנימית רצה מההתחלה.For every round of the outer loop, the inner one runs from the start.
לולאות מקוננותNested loops
כך בונים טבלאות וצורות.That's how tables and shapes are built.
לולאות מקוננותNested loops
מדף, צעד 5: רשימה ממוספרתShelf, step 5: a numbered list
הצעד של היום בפרויקט מדףToday's step in the Shelf project
כמה פעמים תודפס המילה?How many times is the word printed?
33
44
55
אינסוףForever
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.
44
נכון: מ-0 ועד לפני 4.Right: from 0 up to before 4.
מה למדנוWhat we learned
לולאה: קוד שחוזר שוב ושוב:loop: Code that repeats:
שלושה חלקים בסוגריים: התחלה (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++).
צובר מתחיל לפני הלולאה, ובכל סיבוב מוסיפים לו.An accumulator starts before the loop, and each round adds to it.
while בודק לפני כל סיבוב, ומתאים כשלא יודעים כמה סיבובים יהיו.while checks before every round, and suits cases where you don't know how many rounds there'll be.