Booleans and selection: if/else and logic
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · تحتاج هذه الصفحة إلى متصفح حديث (يدعم SharedArrayBuffer). يرجى تحديث Chrome أو Edge أو Firefox أو Safari إلى أحدث إصدار.
English
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.
العربية
المنطقيات والاختيار
- المنطقية هي قيمة تكون إما
trueأوfalse. - البرامج تتخذ القرارات عن طريق اختبار منطقية وتشغيل أكواد مختلفة.
- في هذه الدرس ستتعلم
if/else if/elseوكيفية بناء الشروط.
English
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
}
}
English
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");
}
}
}
English
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
}
}
English
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.
العربية
القصر المسبق
- تقرأ 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
}
}
English
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)
}
}
English
Common mistakes
- A condition must be a
boolean;if (x = 5)does not compile. - Each
switchcase needs abreak;.
العربية
أخطاء شائعة
- يجب أن يكون الشرط من نوع
boolean؛ لا يقومif (x = 5)بالتجميع. - كل حالة
switchتحتاج إلىbreak;.
English
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، أو أكمل الدالة المعروضة.
- اضغط تشغيل للمعالجة والتشغيل، ثم تحقق من الإجابة.
- كودك يُعالج ويُشغل على الخادم، لذا حتى التشغيل الأول سريع.
Explore · استكشف
Boolean logic (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.
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 ==.
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 &&.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.