חלק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.
הנקודות החשובותKey points
- תנאי: שאלה של כן או לא שהתוכנית בודקת, למשל
age >= 18.condition: A yes-or-no question the program checks, likeage >= 18. - ביטוי בוליאני: ביטוי שהערך שלו הוא
trueאוfalse:boolean expression: An expression whose value istrueorfalse: - Java בודקת את התנאים מלמעלה למטה, ומבצעת רק את הענף הראשון שהתנאי שלו
true.Java checks the conditions top to bottom and runs only the first branch whose condition istrue. elseתופס את כל השאר.elsecatches everything else.==שווה,!=שונה,< <= > >=.==equal,!=not equal,< <= > >=.- מחברים:
&&וגם,||או,!לא.Joiners:&&and,||or,!not. - ב-switch החדש כל
caseעם->מריץ רק את עצמו (בליbreak), אפשר לשים כמה ערכים בשורה, ואפשר לשמור את התוצאה במשתנה.In the new switch eachcasewith->runs only itself (nobreak), several values can share a line, and the result can go into a variable. defaultלכל השאר.defaultfor everything else.- כשענף ב-switch צריך כמה שורות, פותחים בלוק ומחזירים עם
yield.When a switch branch needs several lines, open a block and hand back the value withyield.
מונחיםTerms
- תנאיcondition: שאלה של כן או לא שהתוכנית בודקת, למשל
age >= 18. התשובה היאtrueאוfalse.A yes-or-no question the program checks, likeage >= 18. The answer istrueorfalse. - אופרטור לוגי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"; ... };.Aswitchwhere everycaseuses->: 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 istrueorfalse: a comparison, a mix of comparisons, or abooleanvariable. - קיצור דרך (short-circuit)short-circuit: ב-
&&אם הצד השמאליfalse, Java לא בודקת את הימני (התשובה כבר ידועה). ב-||אם השמאליtrue.With&&, if the left side isfalseJava doesn't check the right (the answer is already known). With||, if the left istrue. - תנאיcondition: שאלה של כן או לא שהתוכנית בודקת, למשל
age >= 18. התשובה היאtrueאוfalse.A yes-or-no question the program checks, likeage >= 18. The answer istrueorfalse. - ביטוי בוליאניboolean expression: ביטוי שהערך שלו הוא
trueאוfalse: השוואה, שילוב של השוואות, או משתנהboolean.An expression whose value istrueorfalse: a comparison, a mix of comparisons, or abooleanvariable. - אופרטור לוגי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 isfalseJava doesn't check the right (the answer is already known). With||, if the left istrue. - switch עם חציםswitch expression:
switchשבו כלcaseכתוב עם->: רק הענף המתאים רץ, ואפשר להחזיר ממנו ערך:String s = switch (day) { case 1 -> "Sun"; ... };.Aswitchwhere everycaseuses->: 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 |
|---|