תנאים ו-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.
01המונחים של המפגשThis session's terms
לפני הקוד, המילים. כל מונח כאן מקושר גם במילון של הקורס.
Before the code, the words. Every term here is also linked in the course glossary.
- תנאי: שאלה של כן או לא שהתוכנית בודקת, למשל
age >= 18. התשובה היאtrueאוfalse.condition: A yes-or-no question the program checks, likeage >= 18. The answer istrueorfalse. - ביטוי בוליאני: ביטוי שהערך שלו הוא
trueאוfalse: השוואה, שילוב של השוואות, או משתנהboolean.boolean expression: 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): ב-
&&אם הצד השמאליfalse, Java לא בודקת את הימני (התשובה כבר ידועה). ב-||אם השמאליtrue.short-circuit: With&&, if the left side isfalseJava doesn't check the right (the answer is already known). With||, if the left istrue. - switch עם חצים:
switchשבו כלcaseכתוב עם->: רק הענף המתאים רץ, ואפשר להחזיר ממנו ערך:String s = switch (day) { case 1 -> "Sun"; ... };.switch expression: Aswitchwhere everycaseuses->: only the matching branch runs, and it can return a value:String s = switch (day) { case 1 -> "Sun"; ... };. - אופרטור טרנרי:
תנאי ? ערך1 : ערך2: if קצר שמחזיר ערך.String s = n > 0 ? "plus" : "not plus";.ternary operator:condition ? value1 : value2: a short if that returns a value.String s = n > 0 ? "plus" : "not plus";.
02if, else if, elseif, else if, else
Java בודקת את התנאים מלמעלה למטה, ומבצעת רק את הענף הראשון שהתנאי שלו true. else תופס את כל השאר.
Java checks the conditions top to bottom and runs only the first branch whose condition is true. else catches everything else.
public class Main {
public static void main(String[] args) {
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("סוף");
}
}public class Main {
public static void main(String[] args) {
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");
}
}B סוף
B Done
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
03השוואות ואופרטורים לוגייםComparisons and logical operators
== שווה, != שונה, < <= > >=. מחברים: && וגם, || או, ! לא. ב-&& אם הצד הראשון false, השני לא נבדק בכלל (קיצור דרך (short-circuit)).
== equal, != not equal, < <= > >=. Joiners: && and, || or, ! not. With &&, if the first side is false the second isn't even checked (short-circuit).
public class Main {
public static void main(String[] args) {
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);
}
}true true false
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
04switch עם חצים מחזיר ערךA switch with arrows returns a value
ב-switch החדש כל case עם -> מריץ רק את עצמו (בלי break), אפשר לשים כמה ערכים בשורה, ואפשר לשמור את התוצאה במשתנה. default לכל השאר.
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 for everything else.
public class Main {
public static void main(String[] args) {
int day = 6;
String kind = switch (day) {
case 1, 2, 3, 4, 5 -> "יום חול";
case 6, 7 -> "סוף שבוע";
default -> "?";
};
System.out.println(kind);
}
}public class Main {
public static void main(String[] args) {
int day = 6;
String kind = switch (day) {
case 1, 2, 3, 4, 5 -> "weekday";
case 6, 7 -> "weekend";
default -> "?";
};
System.out.println(kind);
}
}סוף שבוע
weekend
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
05yield ואופרטור טרנריyield and the ternary operator
כשענף ב-switch צריך כמה שורות, פותחים בלוק ומחזירים עם yield. ל-if קצר שמחזיר ערך יש את האופרטור הטרנרי: תנאי ? אם כן : אם לא.
When a switch branch needs several lines, open a block and hand back the value with yield. For a short if that returns a value there's the ternary operator: condition ? if yes : if no.
public class Main {
public static void main(String[] args) {
int stars = 4;
String word = switch (stars) {
case 5 -> "מושלם";
case 4 -> {
String w = "טוב מאוד";
yield w + "!";
}
default -> "בסדר";
};
System.out.println(word);
String parity = stars % 2 == 0 ? "even" : "odd";
System.out.println(parity);
}
}public class Main {
public static void main(String[] args) {
int stars = 4;
String word = switch (stars) {
case 5 -> "perfect";
case 4 -> {
String w = "very good";
yield w + "!";
}
default -> "ok";
};
System.out.println(word);
String parity = stars % 2 == 0 ? "even" : "odd";
System.out.println(parity);
}
}טוב מאוד! even
very good! even
צפו בהרצה צעד אחר צעדWatch it run, step by step
מה קורה עכשיוWhat happens now
הקופסאות (משתנים)The boxes (variables)
מסך הפלטOutput screen
06הצעד של היום בפרויקט מדףToday's step in the Shelf project
לספר יש int stars (כאן 4). בעזרת switch עם חצים, תרגמו ל-Masterpiece (5), Great (4), Good (3), Meh (1 או 2), ואחרת No rating. הדפיסו Rating: Great. אם stars מחוץ ל-1–5, הדפיסו לפני כן Invalid stars! (השתמשו ב-if).
A book has int stars (here 4). With an arrow switch translate it to Masterpiece (5), Great (4), Good (3), Meh (1 or 2), otherwise No rating. Print Rating: Great. If stars is outside 1–5, first print Invalid stars! (use an if).
את הצעד הזה כותבים בעמוד התרגול (התרגיל המסומן כצעד בפרויקט). בעמוד הפתרונות יש פתרון מלא עם סימולציה שמריצה אותו.
You write this step on the practice page (the exercise marked as a project step). The solutions page has a full solution with a simulation that runs it.
סיימתם את ההרצאה!Lecture complete!
עכשיו המצגת לחזרה מהירה, או ישר לתרגול.
Next: the slides for a quick review, or straight to practice.