Booleans and selection: if/else and logic · בווליאנים בחירה: if/else ולוגיקה
Booleans and selection
- A boolean is a value that is either
trueorfalse. - Programs make choices by testing a boolean and running different code.
- In this lesson you learn
if/else if/elseand how to build conditions.
Booleans ובחירה
- Boolean הוא ערך שהוא או
trueאוfalse. - תוכניות מקבלות החלטות על ידי בדיקת boolean והפעלת קוד שונה.
- בשיעור זה תלמד ב
if/else if/elseואיך לבנות תנאים.
Relational operators
- These compare two values and give a
boolean:<less than,>greater than<=less or equal,>=greater or equal==equal,!=not equal
- Note:
=puts a value into a variable, but==tests if two values are equal.
מפעילים יחסיים
- אלו משווים שני ערכים ומחזירים
boolean:<פחות מ,>גדול מ<=קטן או שווה ל,>=גדול או שווה ל==שווה ל,!=לא שווה ל
- הערה:
=מכניס ערך למשתנה, אך==בודק אם שני ערכים שווים זה לזה.
public class Main {
public static void main(String[] args) {
int score = 75;
System.out.println(score > 50); // true
System.out.println(score == 100); // false
System.out.println(score != 75); // false
}
}
if / else if / else
ifruns a block only when its condition istrue.else ifchecks another condition when the ones before werefalse.elseruns when none of the conditions were true.
if / else if / else
ifמפעיל בלוק רק כאשר התנאי שלו הואtrue.else ifבודק תנאי נוסף כאשר התנאים הקודמים היוfalse.elseמפעיל כשהם אין תנאי שהיה אמת.
public class Main {
public static void main(String[] args) {
int n = -4;
if (n > 0) {
System.out.println("positive");
} else if (n < 0) {
System.out.println("negative");
} else {
System.out.println("zero");
}
}
}
Logical operators
&&means and: true only when both sides are true.||means or: true when at least one side is true.!means not: it flipstruetofalseandfalsetotrue.
אופרטורים לוגיים
&&מתאר ו: אמת רק כאשר שתי הצדדים הם אמת.||מתאר או: אמת כאשר לפחות אחד מהצדדים הוא אמת.!מתאר לא: הוא הופךtrueל-falseו-falseל-true.
public class Main {
public static void main(String[] args) {
int age = 16;
boolean teen = age >= 13 && age <= 19;
System.out.println(teen); // true
System.out.println(!teen); // false
System.out.println(age < 13 || age > 19); // false
}
}
Short-circuit
- Java reads
&&and||from left to right and stops early when it can. - With
&&, if the left side isfalse, the answer is alreadyfalse, so the right side is skipped. - With
||, if the left side istrue, the answer is alreadytrue, so the right side is skipped. - This lets you check something is safe before you use it.
קצר-מסלול (Short-circuit)
- Java קורא את
&&ואת||מ-שמאל לימין ועוצר מוקדם כשהוא יכול. - עם
&&, אם הצד השמאלי הואfalse, התוצאה כבר ידועה לfalse, ולכן הצד הימני מועבר. - עם
||, אם הצד השמאלי הואtrue, התוצאה כבר ידועה לtrue, ולכן הצד הימני מועבר. - דבר זה מאפשר לבדוק משהו בטיחותי לפני השימוש בו.
public class Main {
public static void main(String[] args) {
int x = 0;
// Left side is false, so x / x is never run (no error).
boolean ok = (x != 0) && (10 / x > 1);
System.out.println(ok); // false
}
}
The == vs .equals() String trap
- For
intandboolean,==works fine. - For
String,==checks if they are the same object in memory, not the same letters. - Two Strings with the same letters can still be different objects, so
==may befalse. - Always compare Strings with
.equals(...).
פעח המיתוס: == מול .equals() במתחזיות טקסט
- עבור
intו-boolean,==עובד בצורה תקינה. - עבור
String,==בודק האם מדובר באותו אובייקט בזיכרון, ולא רק באותות זהות. - שתי מחרוזות עם אותם האותיות עדיין יכולות להיות אובייקטים שונים, ולכן
==עשוי להיותfalse. - תמיד השוו מחרוזות באמצעות
.equals(...).
public class Main {
public static void main(String[] args) {
String a = "cat";
String b = new String("cat");
System.out.println(a == b); // false (different objects)
System.out.println(a.equals(b)); // true (same letters)
}
}
Common mistakes
- A condition must be a
boolean;if (x = 5)does not compile. - Each
switchcase needs abreak;.
טעויות נפוצות
- תנאי חייב להיות
boolean; שימוש ב-if (x = 5)לא יעבור קומפילציה. - כל מקרה
switchדורשbreak;.
Now you try
- Each task pre-fills the class skeleton — write your code inside main, or complete the method shown.
- Press Run to compile and run, then Check answer.
- Your code compiles and runs on the server, so even the first run is fast.
כעת תנסו בעצמכם
- כל משימה ממלאת מראש את שלד המחלקה — כתוב את הקוד שלך בתוך main, או השלם את השיטה המוצגת.
- לחץ על Run כדי להרכיב ולהריץ, ולאחר מכן לחץ על Check answer.
- הקוד שלך נרכב ונרץ על השרת, ולכן גם ההפעלה הראשונה היא מהירה.
Boolean logic (and/or/not) · לוגיקה בוליאנית (and/or/not)
&&, ||, ! combine true/false values — see the truth table. · &&, ||, ! משלבים ערכים נכונים/שגויים — ראו את טבלת האמת.
The variable n is -4. Use if / else if / else to print exactly one word: positive if n > 0, negative if n < 0, or zero otherwise. · המשתנה n הוא -4. השתמשו בif / else if / else כדי להדפיס מילה אחת בדיוק: positive אם n > 0, negative אם n < 0, או zero אחרת.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Complete equal(String a, String b) so it returns true when the two Strings have the same letters. Use .equals, not ==. · השלימו את equal(String a, String b) כך שתחזיר את true כאשר לשתי המחרוזות אותו סדר אותיות. השתמשו ב.equals, לא ב==.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Complete inRange(int x) so it returns true when x is between 10 and 20, including both ends (10 and 20 count). Use &&. · השלימו את inRange(int x) כך שתחזיר את true כאשר x נמצא בין 10 ל-20, כולל שתי הקצוות (10 ו-20 נספרים). השתמשו ב&&.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.