You are on page 1of 20

‫أساسيات البرمجة‬

Chapter(3):

Selection Statements
Contents: ‫محتويات المحاضرة‬
3.1. Introduction

3.2. boolean Data Type and Operations


 boolean data type

 Comparison operators
 Boolean Operators
 Unconditional vs. Conditional Boolean Operators

 Examples

3.3. Selection Statements


3.1. Introduction
 Java provides selection statements that let you choose
between actions.
‫تعليمات االختيار تُستخدم للتحكم بتسلسل تنفيذ البرنامج‬

 Selection statements use conditions (‫شروط‬/‫)قيود‬,


Conditions are Boolean expressions (‫)عبارت منطقية‬

 This chapter first introduces Boolean types, values,


operators, and expressions
3.2. boolean Data Type
 boolean data type is used to declare Boolean
variables (‫)تعريف المتغيرات المنطقية‬
 boolean type consists of two literal values:
true and false.
boolean ‫ تعريف متغيرات من النوع‬: ‫مثال‬
boolean ligthsOn = true;
‫ لهذا لمتغير‬true ‫تعريف متغير واسناد القيمة‬//
boolean ligthsOff = false;
‫ لهذا لمتغير‬false ‫تعريف متغير واسناد القيمة‬//
System.out.println(“the value of lightsOn is ” + ligthsOn);

System.out.println(“the value of lightsOff is ” + ligthsOff);


3.2. Comparison Operators (‫)عالمات المقارنة‬
 Java provides six comparison operators (also known
as relational operators), which can be used to compare
two values.
 The result of the comparison is a boolean value:
true or false
Operator Name

< less than ‫أقل من‬


<= less than or equal to ‫أقل من أو يساوى‬
> grater than ‫أكبر من‬
>= grater than or equal to ‫أكبر من أو يساوى‬
== equal to ‫يساوى‬
!= not equal to ‫ال يساوى‬
3.2. boolean Data Type- Comparison Operators
 Example(1): Comparison Operators with numbers

Operator Name example Answer

< less than 1<2 true

<= less than or equal to 1 <= 2 true

> grater than 1>2 false

>= grater than or equal to 1 >= 2 false

== equal to 1 == 2 false

!= not equal to 1 != 2 true


3.2. boolean Data Type- Comparison Operators
Example(2): Comparison Operators with character
 Comparing characters is the same as comparing the
Unicodes of the characters
Operator Name example Answer
< less than „a‟ < „A‟ false

<= less than or equal to „a‟ < „A‟ false

> grater than „a‟ > „A‟ true

>= grater than or equal to „a‟ >= „A‟ true

== equal to „a‟ == „A‟ false

!= not equal to „a‟ != „A‟ true


3.2. boolean Operators (‫)العالمات المنطقية‬
 Boolean operators, also known as logical operators
‫األدوات المنطقية أو العالمات المنطقية تُستخدم لكتابة الشروط أو القيود‬
‫الموجودة بالبرامج‬

Operator Name Description

! Not ‫اداة النفى‬ logical Negation

&& And ‫اداة االتحاد‬ logical Conjunction

|| or logical disjunction

^ exclusive or logical exclusion


3.2. boolean Operators (‫)العالمات المنطقية‬
Operator Name Description
1 ! Not ‫اداة النفى‬ logical Negation

The not (!) operator negates true to false and false to


true
‫ كما موضح ادناه‬،‫تستخدم هذه العالمة للنفى وعكس القيمة المستخدمة‬

Table 3.3. Truth Table for Operator ! ‫جدول الصواب‬


p !p example

true false !(1 > 2) is true

false true !(1 > 0) is false


3.2. boolean Operators (‫)العالمات المنطقية‬
Operator Name Description
2 && and ‫اداة االتحاد‬ logical conjunction

The and (&&) of two Boolean operands is true if and only if


both operands are true
true ‫هو أن تكون قيمة جميع المعامالت المنطقية‬true ‫الشرط األساسى لتكون قيمتها‬
Table 3.4. Truth Table for Operator && ‫جدول الصواب‬
p1 p2 p1 && p2 example
false false false (2 > 3) && (5 > 5) is false

false true false (2 > 3) && (6 > 5) is false

true false false (6 > 5) && (2 > 3) is false


true true true (3 > 2) && (6 > 5) is true
3.2. boolean Operators (‫)العالمات المنطقية‬
Operator Name Description
3 II or logical disjunction

The or (||) of two boolean operands is true if at least one of


the operands is true
true ‫ هو أن تكون قيمة أحد المعامالت المنطقية‬true ‫الشرط األساسى لتكون قيمتها‬
Table 3.5. Truth Table for Operator || ‫جدول الصواب‬
p1 p2 p1 || p2 example
false false false (2 > 3) || (5 > 5) is false

false true true (2 > 3) || (6 > 5) is true

true false true (6 > 5) || (2 > 3) is true


true true true (3 > 2) || (6 > 5) is true
3.2. boolean Operators (‫)العالمات المنطقية‬
Operator Name Description
4 ^ exclusive or logical exclusion

The exclusive or (^) of two boolean operands is true if and


only if the two operands have different Boolean values
‫الشرط األساسى لتنفيذها هو اختالف قيمة المعامالت المنطقية(مثال فى حالة وجود‬
(false ‫ واآلخر‬true ‫معاملين يكون أحدها قيمته‬
Table 3.5. Truth Table for Operator ^ ‫جدول الصواب‬
p1 p2 p1 ^ p2 example
false false false (2 > 3) ^ (5 > 5) is false

false true true (2 > 3) ^ (5 > 1) is true

true false true (3 > 2) ^ (5 > 5) is true


true true false (3 > 2) ^ (5 > 1) is false
‫‪3.2. boolean Operators‬‬ ‫)العالمات المنطقية(‬
‫مثال(‪ :(1‬أكتب برنامج يقوم بتعريف متغير من النوع (‪ (integer‬وإسناد القيمة ‪18‬‬
‫لهذا المتغير‪ ،‬بحيث يقوم البرنامج االتى‪:‬‬
‫(‪ (1‬إختبار حاصل قسمة هذا العدد على ‪ 2‬و ‪ 3‬وطباعة النتيجة‬
‫(‪ (2‬أختبار حاصل قسمة هذا العدد على ‪ 2‬أو ‪ 3‬وطباعة النتيجة‬
‫‪TestBoolean.java‬‬
‫{ ‪public class TestBoolean‬‬
‫{ )‪public static void main(String[ ] args‬‬
‫;‪int num =10‬‬

‫‪// use and operator‬‬ ‫اختبار قسمة العدد على العددين ‪2‬و‪ 3‬معا‬

‫;)‪boolean output = (num % 2 == 0)&&(num %3 == 0‬‬


3.2. boolean Operators (1(‫مثال‬
TestBoolean.java
System.out.println(“is number “+ num);

System.out.println(“divisible by 2 and 3 ? “ + output);

// use or operator 3 ‫ أو‬2 ‫اختبار قسمة العدد على أى من العددين‬


output = (num % 2 == 0) || (num %3 == 0);

System.out.println(“divisible by 2 or 3 ? “ + output);

} //end of main
} //end of class
3.2. boolean Operators (1(‫مثال‬

:‫ناتج التنفيذ‬

is number 18

divisible by 2 and 3 ? true

divisible by 2 or 3 ? true
‫‪3.2. boolean Operators‬‬ ‫)العالمات المنطقية(‬
‫مثال(‪ :(2‬أكتب برنامج يطلب من المستخدم اخال عدد صحيح من النوع (‪(integer‬‬
‫يمثل السنة‪ ،‬يقوم البرنامج بإختبار السنة التى تم ادخالها اذا كانت سنة كبيسة (السنة‬
‫التى يكون عدد أيامها ‪ 366‬يوم(‬

‫يتم حساب السنة الكبيسة بتحقق الشرط اآليت‪:‬‬


‫اذا كانت السنة المدخلة تقبل القسمة على العدد ‪ 4‬وال تقبل القسمة على العدد‬
‫‪100‬‬
3.2. boolean Operators (2(‫مثال‬
LeapYear.java
import java.util.Scanner; //Scanner ‫تحديد مكان وجود الـ‬
public class LeapYear {
public static void main(String[ ] args) {
// create Scanner
Scanner scann = new Scanner(System.in);
System.out.print(“Enter a year:“);
int year = scann.nextInt();
boolean isLeap = (year % 4 == 0)&&(year % 100 !=0);

System.out.println(year + “is Leap year? “ + isLeap);


} //end of main
} //end of class
3.2. boolean Operators (2(‫مثال‬

:‫ناتج التنفيذ‬

1 Enter a year : 2008

2004 is Leap year ? true

:‫ناتج التنفيذ‬

2 Enter a year : 200

200 is Leap year ? false


3.2. Unconditional vs. Conditional Boolean Operators
 Java also provides the & and | operators
 works exactly the same as the && and the || operator
 & is referred to as the unconditional AND operator
 | is referred to as the unconditional OR operator
 In some rare situations, you can use the & and | operators
to guarantee that the right-hand operand is evaluated
regardless of whether the left-hand operand is true or
false
‫فى بعض الحاالت يتم استخدامها لتنفيذ المعامالت التى تكون بإتجاه اليمين مهما‬
:‫ مثال‬،‫كان ناتج تنفيذ المعامالت الموجودة على اليسار‬
(width < 2) & (height-- < 2)
will be decremented regardless of whether width is less
than 2 or not. (‫)سيتم تغيير قيمة المتغير فى حالة تنفيذ الشرط أو عدم تنفيذه‬
3.3. Selection Statements (‫)تعليمات االختيار‬
 Java has several types of selection statements:
 simple if statements
 if ... else statements
 nested if statements
 switch statements
 conditional expressions

You might also like