נבנה על ידיBuilt by Ariel Yohimovich

חזרה למפגשBack
JavaJava

לולאותLoops

מפגש 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.

מה נעשה היום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
נושא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:
נושא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.
שאלה לכיתה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");

    לולאת 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
    נושא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.
    שאלה לכיתה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);

      צובר: סכום בתוך לולאה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
      נושא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?
      נושאTopic 5

      for-each, break ו-continuefor-each, break and continue

      for-each, break ו-continuefor-each, break and continue

      • for-each עובר על כל פריט.for-each goes over every item.
      • continue מדלג לסיבוב הבא, ו-break יוצא מהלולאה מיד.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();

        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
        נושא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.
        שאלה לכיתה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();
        }

          לולאות מקוננות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
          *
          **
          ***
          נושאTopic 7

          פרויקט מדףShelf project

          הצעד של היום בפרויקט מדףToday's step in the Shelf project

          • מדף, צעד 5: רשימה ממוספרתShelf, step 5: a numbered list

          לולאה: קוד שחוזר שוב ושוב: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:

          שלושה חלקים בסוגריים: התחלה (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.

          צובר מתחיל לפני הלולאה, ובכל סיבוב מוסיפים לו.An accumulator starts before the loop, and each round adds to it.

          בסוף הוא מחזיק את התוצאה.At the end it holds the result.

          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?

          for-each עובר על כל פריט.for-each goes over every item.

          continue מדלג לסיבוב הבא, ו-break יוצא מהלולאה מיד.continue skips to the next round, and break leaves the loop at once.

          על כל סיבוב של הלולאה החיצונית, הפנימית רצה מההתחלה.For every round of the outer loop, the inner one runs from the start.

          כך בונים טבלאות וצורות.That's how tables and shapes are built.

          מדף, צעד 5: רשימה ממוספרתShelf, step 5: a numbered list

          כמה פעמים תודפס המילה?How many times is the word printed?

          • 33
          • 44
          • 55
          • אינסוףForever

          44

          מה למדנו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.
          • for-each עובר על כל פריט.for-each goes over every item.
          • על כל סיבוב של הלולאה החיצונית, הפנימית רצה מההתחלה.For every round of the outer loop, the inner one runs from the start.

          עכשיו לתרגולNow, practice

          7 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית7 exercises and 2 challenges, auto-checked
          הערות מרצהSpeaker notes

          נבנה על ידיBuilt by Ariel Yohimovich