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

JavaJava
4
חלקPart 1: יסודותFoundations כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

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

if, else if ו-else, אופרטורים לוגיים, switch עם חצים (->) כביטוי שמחזיר ערך, ו-yield.

if, else if and else, logical operators, switch with arrows (->) as an expression that returns a value, and yield.

או Ctrl+Por Ctrl+P

הנקודות החשובותKey points

  • תנאי: שאלה של כן או לא שהתוכנית בודקת, למשל 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:
  • 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.
  • ב-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.

מונחיםTerms

  • תנאיcondition: שאלה של כן או לא שהתוכנית בודקת, למשל age >= 18. התשובה היא true או false.A yes-or-no question the program checks, like age >= 18. The answer is true or false.
  • אופרטור לוגיlogical operator: מחבר תנאים: && (וגם: שניהם נכונים), || (או: לפחות אחד), ! (לא: הופך).Joins conditions: && (and: both true), || (or: at least one), ! (not: flips).
  • switch עם חציםswitch expression: switch שבו כל case כתוב עם ->: רק הענף המתאים רץ, ואפשר להחזיר ממנו ערך: String s = switch (day) { case 1 -> "Sun"; ... };.A switch where every case uses ->: only the matching branch runs, and it can return a value: String s = switch (day) { case 1 -> "Sun"; ... };.
  • אופרטור טרנריternary operator: תנאי ? ערך1 : ערך2: if קצר שמחזיר ערך. String s = n > 0 ? "plus" : "not plus";.condition ? value1 : value2: a short if that returns a value. String s = n > 0 ? "plus" : "not plus";.
  • ביטוי בוליאניboolean expression: ביטוי שהערך שלו הוא true או false: השוואה, שילוב של השוואות, או משתנה boolean.An expression whose value is true or false: a comparison, a mix of comparisons, or a boolean variable.
  • קיצור דרך (short-circuit)short-circuit: ב-&& אם הצד השמאלי false, Java לא בודקת את הימני (התשובה כבר ידועה). ב-|| אם השמאלי true.With &&, if the left side is false Java doesn't check the right (the answer is already known). With ||, if the left is true.
  • תנאיcondition: שאלה של כן או לא שהתוכנית בודקת, למשל age >= 18. התשובה היא true או false.A yes-or-no question the program checks, like age >= 18. The answer is true or false.
  • ביטוי בוליאניboolean expression: ביטוי שהערך שלו הוא true או false: השוואה, שילוב של השוואות, או משתנה boolean.An expression whose value is true or false: a comparison, a mix of comparisons, or a boolean variable.
  • אופרטור לוגיlogical operator: מחבר תנאים: && (וגם: שניהם נכונים), || (או: לפחות אחד), ! (לא: הופך).Joins conditions: && (and: both true), || (or: at least one), ! (not: flips).
  • קיצור דרך (short-circuit)short-circuit: ב-&& אם הצד השמאלי false, Java לא בודקת את הימני (התשובה כבר ידועה). ב-|| אם השמאלי true.With &&, if the left side is false Java doesn't check the right (the answer is already known). With ||, if the left is true.
  • switch עם חציםswitch expression: switch שבו כל case כתוב עם ->: רק הענף המתאים רץ, ואפשר להחזיר ממנו ערך: String s = switch (day) { case 1 -> "Sun"; ... };.A switch where every case uses ->: only the matching branch runs, and it can return a value: String s = switch (day) { case 1 -> "Sun"; ... };.
  • אופרטור טרנריternary operator: תנאי ? ערך1 : ערך2: if קצר שמחזיר ערך. String s = n > 0 ? "plus" : "not plus";.condition ? value1 : value2: a short if that returns a value. String s = n > 0 ? "plus" : "not plus";.

Cheatsheet

if, else if, elseif, else if, else
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");
השוואות ואופרטורים לוגייםComparisons and logical operators
int age = 16;
boolean withParent = true;
boolean canEnter = age >= 18 || withParent;
System.out.println(canEnter);
System.out.println(age > 12 && age < 20);
System.out.println(!canEnter);
switch עם חצים מחזיר ערךA switch with arrows returns a value
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);
yield ואופרטור טרנריyield and the ternary operator
int stars = 4;
String word = switch (stars) {
    case 5 -> "מושלם";
    case 4 -> {
        String w = "טוב מאוד";
        yield w + "!";
    }
    default -> "בסדר";
};
System.out.println(word);int stars = 4;
String word = switch (stars) {
    case 5 -> "perfect";
    case 4 -> {
        String w = "very good";
        yield w + "!";
    }
    default -> "ok";
};
System.out.println(word);

שגיאות שפגשנוErrors we met

שגיאהErrorמה זה אומרWhat it meansמה עושיםWhat to do

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