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

חזרה למפגשBack
JavaJava

תנאים ו-switch החדשConditions & the new switch

מפגש 4: if, else if ו-else, אופרטורים לוגיים, switch עם חצים (->) כביטוי שמחזיר ערך, ו-yield.Session 4: if, else if and else, logical operators, switch with arrows (->) as an expression that returns a value, and yield.

מה נעשה היוםToday's plan

  • מונחיםTerms
  • if, else if, elseif, else if, else
  • השוואות ואופרטורים לוגייםComparisons and logical operators
  • switch עם חצים מחזיר ערךA switch with arrows returns a value
  • yield ואופרטור טרנריyield and the ternary operator
  • פרויקט מדףShelf project
נושאTopic 1

מונחיםTerms

המונחים של המפגשThis session's terms

  • תנאי: שאלה של כן או לא שהתוכנית בודקת, למשל age >= 18.condition: A yes-or-no question the program checks, like age >= 18.
  • ביטוי בוליאני: ביטוי שהערך שלו הוא true או false:boolean expression: An expression whose value is true or false:
  • אופרטור לוגי: מחבר תנאים:logical operator: Joins conditions:
  • קיצור דרך (short-circuit): ב-&& אם הצד השמאלי false, Java לא בודקת את הימני (התשובה כבר ידועה).short-circuit: With &&, if the left side is false Java doesn't check the right (the answer is already known).
  • switch עם חצים: switch שבו כל case כתוב עם ->:switch expression: A switch where every case uses ->:
  • אופרטור טרנרי: `תנאי ?ternary operator: `condition ?
נושאTopic 2

if, else if, elseif, else if, else

if, else if, elseif, else if, else

  • Java בודקת את התנאים מלמעלה למטה, ומבצעת רק את הענף הראשון שהתנאי שלו true.Java checks the conditions top to bottom and runs only the first branch whose condition is true.
  • else תופס את כל השאר.else catches everything else.
שאלה לכיתהQuestion for the class

מה ידפיס הקוד?What will this print?

main
int score = 82;
if (score >= 90) {
    System.out.println("A");
} else if (score >= 80) {
    System.out.println("B");
} else {
    System.out.println("C");
}
System.out.println("סוף");
int score = 82;
if (score >= 90) {
    System.out.println("A");
} else if (score >= 80) {
    System.out.println("B");
} else {
    System.out.println("C");
}
System.out.println("Done");

    if, else if, elseif, else if, else

    Main.java · main
    int score = 82;
    if (score >= 90) {
        System.out.println("A");
    } else if (score >= 80) {
        System.out.println("B");
    } else {
        System.out.println("C");
    }
    System.out.println("סוף");
    int score = 82;
    if (score >= 90) {
        System.out.println("A");
    } else if (score >= 80) {
        System.out.println("B");
    } else {
        System.out.println("C");
    }
    System.out.println("Done");
    פלטOutput
    B
    סוף
    B
    Done
    נושאTopic 3

    השוואות ואופרטורים לוגייםComparisons and logical operators

    השוואות ואופרטורים לוגייםComparisons and logical operators

    • == שווה, != שונה, < <= > >=.== equal, != not equal, < <= > >=.
    • מחברים: && וגם, || או, ! לא.Joiners: && and, || or, ! not.
    • ב-&& אם הצד הראשון false, השני לא נבדק בכלל (קיצור דרך (short-circuit)).With &&, if the first side is false the second isn't even checked (short-circuit).
    נושאTopic 4

    switch עם חצים מחזיר ערךA switch with arrows returns a value

    switch עם חצים מחזיר ערךA switch with arrows returns a value

    • ב-switch החדש כל case עם -> מריץ רק את עצמו (בלי break), אפשר לשים כמה ערכים בשורה, ואפשר לשמור את התוצאה במשתנה.In the new switch each case with -> runs only itself (no break), several values can share a line, and the result can go into a variable.
    • default לכל השאר.default for everything else.
    שאלה לכיתהQuestion for the class

    מה ידפיס הקוד?What will this print?

    main
    int day = 6;
    String kind = switch (day) {
        case 1, 2, 3, 4, 5 -> "יום חול";
        case 6, 7 -> "סוף שבוע";
        default -> "?";
    };
    System.out.println(kind);
    int day = 6;
    String kind = switch (day) {
        case 1, 2, 3, 4, 5 -> "weekday";
        case 6, 7 -> "weekend";
        default -> "?";
    };
    System.out.println(kind);

      switch עם חצים מחזיר ערךA switch with arrows returns a value

      Main.java · main
      int day = 6;
      String kind = switch (day) {
          case 1, 2, 3, 4, 5 -> "יום חול";
          case 6, 7 -> "סוף שבוע";
          default -> "?";
      };
      System.out.println(kind);
      int day = 6;
      String kind = switch (day) {
          case 1, 2, 3, 4, 5 -> "weekday";
          case 6, 7 -> "weekend";
          default -> "?";
      };
      System.out.println(kind);
      פלטOutput
      סוף שבוע
      weekend
      נושאTopic 5

      yield ואופרטור טרנריyield and the ternary operator

      yield ואופרטור טרנריyield and the ternary operator

      • כשענף ב-switch צריך כמה שורות, פותחים בלוק ומחזירים עם yield.When a switch branch needs several lines, open a block and hand back the value with yield.
      • ל-if קצר שמחזיר ערך יש את האופרטור הטרנרי: `תנאי ?For a short if that returns a value there's the ternary operator: `condition ?
      • אם כן : אם לא`.if yes : if no`.
      נושאTopic 6

      פרויקט מדףShelf project

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

      • מדף, צעד 4: דירוג במיליםShelf, step 4: a rating in words

      תנאי: שאלה של כן או לא שהתוכנית בודקת, למשל age >= 18.condition: A yes-or-no question the program checks, like age >= 18.

      ביטוי בוליאני: ביטוי שהערך שלו הוא true או false:boolean expression: An expression whose value is true or false:

      אופרטור לוגי: מחבר תנאים:logical operator: Joins conditions:

      קיצור דרך (short-circuit): ב-&& אם הצד השמאלי false, Java לא בודקת את הימני (התשובה כבר ידועה).short-circuit: With &&, if the left side is false Java doesn't check the right (the answer is already known).

      switch עם חצים: switch שבו כל case כתוב עם ->:switch expression: A switch where every case uses ->:

      אופרטור טרנרי: `תנאי ?ternary operator: `condition ?

      Java בודקת את התנאים מלמעלה למטה, ומבצעת רק את הענף הראשון שהתנאי שלו true.Java checks the conditions top to bottom and runs only the first branch whose condition is true.

      else תופס את כל השאר.else catches everything else.

      == שווה, != שונה, < <= > >=.== equal, != not equal, < <= > >=.

      מחברים: && וגם, || או, ! לא.Joiners: && and, || or, ! not.

      ב-&& אם הצד הראשון false, השני לא נבדק בכלל (קיצור דרך (short-circuit)).With &&, if the first side is false the second isn't even checked (short-circuit).

      ב-switch החדש כל case עם -> מריץ רק את עצמו (בלי break), אפשר לשים כמה ערכים בשורה, ואפשר לשמור את התוצאה במשתנה.In the new switch each case with -> runs only itself (no break), several values can share a line, and the result can go into a variable.

      default לכל השאר.default for everything else.

      כשענף ב-switch צריך כמה שורות, פותחים בלוק ומחזירים עם yield.When a switch branch needs several lines, open a block and hand back the value with yield.

      ל-if קצר שמחזיר ערך יש את האופרטור הטרנרי: `תנאי ?For a short if that returns a value there's the ternary operator: `condition ?

      אם כן : אם לא`.if yes : if no`.

      מדף, צעד 4: דירוג במיליםShelf, step 4: a rating in words

      מה ידפיס הקוד?What does the code print?

      • bigbig
      • big ואז mediumbig then medium
      • mediummedium
      • כלוםNothing

      bigbig

      מה הערך של 5 > 3 && 2 > 4?What is 5 > 3 && 2 > 4?

      • truetrue
      • falsefalse
      • שגיאהAn error
      • 55

      falsefalse

      מה ההבדל העיקרי של case 1 -> ... מ-case 1: ...?What's the main difference between case 1 -> ... and case 1: ...?

      • עם חץ אין "נפילה" ל-case הבא, ולא צריך breakWith an arrow there's no "fall-through" into the next case, so no break is needed
      • חץ עובד רק עם מחרוזותArrows only work with strings
      • אין הבדלNo difference
      • עם חץ כל ה-cases רציםWith arrows every case runs

      עם חץ אין "נפילה" ל-case הבא, ולא צריך breakWith an arrow there's no "fall-through" into the next case, so no break is needed

      זוגי או אי-זוגיEven or odd

      • במשתנה n שימו 17.Put 17 in n.

      ציון במיליםA grade in words

      • במשתנה score שימו 73.Put 73 in score.

      כניסה למתקןRide entry

      • למתקן נכנסים מי שגובהו לפחות 140 וגם גילו לפחות 10, או מי שבא עם הורה.You may ride if you're at least 140 tall and at least 10 years old, or if you came with a parent.

      מה למדנוWhat we learned

      • תנאי: שאלה של כן או לא שהתוכנית בודקת, למשל age >= 18.condition: A yes-or-no question the program checks, like age >= 18.
      • Java בודקת את התנאים מלמעלה למטה, ומבצעת רק את הענף הראשון שהתנאי שלו true.Java checks the conditions top to bottom and runs only the first branch whose condition is true.
      • == שווה, != שונה, < <= > >=.== equal, != not equal, < <= > >=.
      • ב-switch החדש כל case עם -> מריץ רק את עצמו (בלי break), אפשר לשים כמה ערכים בשורה, ואפשר לשמור את התוצאה במשתנה.In the new switch each case with -> runs only itself (no break), several values can share a line, and the result can go into a variable.
      • כשענף ב-switch צריך כמה שורות, פותחים בלוק ומחזירים עם yield.When a switch branch needs several lines, open a block and hand back the value with yield.
      • מדף, צעד 4: דירוג במיליםShelf, step 4: a rating in words

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

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

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