Upload
ahmed-hesham
View
42
Download
0
Embed Size (px)
Citation preview
By :
Java Operators
The operators in Java are like the following : 1. Arithme*c operators . 2. Rela*onal operators. 3. Logical operators. 4. Assignment operators.
Arithmetic Operators
Operator Descrip-on Example
+ Addi-on – Adds values on either side of the operator A + B will give 30 -‐ Subtrac-on – Subtracts right hand from le? hand A - B will give -10 * Mul-plica-on -‐ Mul-plies values on either side of the operator A * B will give 200 / Division -‐ Divides le? hand operand by right hand operand B / A will give 2
% Modulus -‐ Divides le? hand operand by right hand operand and returns remainder B % A will give 0
++ Increment -‐ Increases the value of operand by 1 B++ gives 21 -‐-‐ Decrement -‐ Decreases the value of operand by 1 B-- gives 19
Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra. The following table lists the arithmetic operators: Assume integer variable A holds 10 and variable B holds 20, then:
public static void main(String[] args) { int A , B ; A=10 ; B=20 ; System.out.println ( A + B ) ; }
010101010101010101010101
30
Addition Operator
A=10 B=20
public static void main(String[] args) { int A , B ; A=10 ; B=20 ; System.out.println ( A - B ) ; }
010101010101010101010101
-10
Subtraction Operator
A=10 B=20
public static void main(String[] args) { int A , B ; A=10 ; B=20 ; System.out.println ( A * B ) ; }
010101010101010101010101
200
Multiplication Operators
A=10 B=20
public static void main(String[] args) { int A , B ; A=10 ; B=20 ; System.out.println ( B / A ) ; }
010101010101010101010101
2
Divition Operator
A=10 B=20
public static void main(String[] args) { int A , B ; A=10 ; B=20 ; System.out.println ( B % A ) ; }
010101010101010101010101
0
Modulus Operator
A=10 B=20
public static void main(String[] args) { int A , B ; A=10 ; B=20 ; System.out.println ( + + B ) ; }
010101010101010101010101
Increment Operator
A=10 B=20
public static void main(String[] args) { int A , B ; A=10 ; B=20 ; System.out.println ( + + B ) ; }
010101010101010101010101
Increment Operator
A=10 B=21
public static void main(String[] args) { int A , B ; A=10 ; B=20 ; System.out.println ( + + B ) ; }
010101010101010101010101
Increment Operator
A=10 B=21
21
public static void main(String[] args) { int A , B ; A=10 ; B=20 ; System.out.println (B + +) ; }
010101010101010101010101
20
Increment Operator
A=10 B=20
public static void main(String[] args) { int A , B ; A=10 ; B=20 ; System.out.println (B + +) ; }
010101010101010101010101
20
Increment Operator
A=10 B=21
public static void main(String[] args) { int A , B ; A=10 ; B=20 ; System.out.println ( - - B) ; }
010101010101010101010101
Decrement Operator
A=10 B=20
public static void main(String[] args) { int A , B ; A=10 ; B=20 ; System.out.println ( - - B) ; }
010101010101010101010101
Decrement Operator
A=10 B=19
public static void main(String[] args) { int A , B ; A=10 ; B=20 ; System.out.println ( - - B) ; }
010101010101010101010101
Decrement Operator
A=10 B=19
19
public static void main(String[] args) { int A , B ; A=10 ; B=20 ; System.out.println ( B - - ) ; }
010101010101010101010101
Decrement Operator
A=10 B=20
public static void main(String[] args) { int A , B ; A=10 ; B=20 ; System.out.println ( B - - ) ; }
010101010101010101010101
20
Decrement Operator
A=10 B=20
public static void main(String[] args) { int A , B ; A=10 ; B=20 ; System.out.println ( B - - ) ; }
010101010101010101010101
20
Decrement Operator
A=10 B=19
Relational Operators
operator Descrip-on Example
== Checks if the values of two operands are equal or not, if yes then condi-on becomes true. (equal ) (A == B) is not true.
!= Checks if the values of two operands are equal or not, if values are not equal then condi-on becomes true. ( not equal ) (A != B) is true.
> Checks if the value of le? operand is greater than the value of right operand, if yes then condi-on becomes true. ( greater than) (A > B) is not true.
< Checks if the value of le? operand is less than the value of right operand, if yes then condi-on becomes true. ( Less than) (A < B) is true.
>= Checks if the value of le? operand is greater than or equal to the value of right operand, if yes then condi-on becomes true. (A >= B) is not true.
<= Checks if the value of le? operand is less than or equal to the value of right operand, if yes then condi-on becomes true. (A <= B) is true.
There are following relational operators supported by Java language: Assume variable A holds 10 and variable B holds 20 , then:
public static void main(String[] args) { int x , y ; x = 10 ; y = 20 ; System.out.println ( x = = y ) ; }
010101010101010101010101
False
Relational Operators
x = 10 y = 20
public static void main(String[] args) { int x , y ; x = 10 ; y = 20 ; System.out.println ( x ! = y ) ; }
010101010101010101010101
True
Relational Operators
x = 10 y = 20
public static void main(String[] args) { int x , y ; x = 10 ; y = 20 ; System.out.println ( x > y ) ; }
010101010101010101010101
False
Relational Operators
x = 10 y = 20
public static void main(String[] args) { int x , y ; x = 10 ; y = 20 ; System.out.println ( x < y ) ; }
010101010101010101010101
True
Relational Operators
x = 10 y = 20
public static void main(String[] args) { int x , y ; x = 10 ; y = 20 ; System.out.println ( x > = y ) ; }
010101010101010101010101
False
Relational Operators
x = 10 y = 20
public static void main(String[] args) { int x , y ; x = 10 ; y = 20 ; System.out.println ( x < = y ) ; }
010101010101010101010101
True
Relational Operators
x = 10 y = 20
Logical Operators
Operator Descrip-on Example
&& Called Logical AND operator. If both the operands are non-‐zero, then the condi-on becomes true. (A && B) is false.
|| Called Logical OR Operator. If any of the two operands are non-‐zero, then the condi-on becomes true.
(A || B) is true.
! Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condi-on is true then Logical NOT operator will make false.
!(A && B) is true.
The following table lists the logical operators: Assume Boolean variables A holds true and variable B holds false , then:
public static void main(String[] args) { int test = 6 ; If ( test = = 9 ) { System.out.println( “ yes ” ) ; } else { System.out.println( “ this is else ” ) ; } }
010101010101010101010101
this is else
If Statement
test = 6
public static void main(String[] args) { int boy , girl ; boy = 18 ; girl = 68 ; If ( boy > 10 ) { System.out.println(“you can enter”) ; }else{ System.out.println(“you are too young”) } }
010101010101010101010101
You can enter
Logical Operators
boy = 18 girl = 68
public static void main(String[] args) { int boy , girl ; boy = 18 ; girl = 68 ; If ( boy > 10 && girl < 60 ) { System.out.println(“you can enter”) ; }else{ System.out.println(“you are too young”); } }
010101010101010101010101
You are too young
Logical Operators
boy = 18 girl = 68
public static void main(String[] args) { int boy , girl ; boy = 18 ; girl = 68 ; If ( boy > 10 | | girl < 60 ) { System.out.println(“you can enter”) ; }else{ System.out.println(“you are too young”); } }
010101010101010101010101
You can enter
Logical Operators
boy = 18 girl = 68
public static void main(String[] args) { int boy , girl ; boy = 18 ; girl = 68 ; If (!( boy > 10 && girl < 60 )) { System.out.println(“you can enter”) ; }else{ System.out.println(“you are too young”); } }
010101010101010101010101
you can enter
Logical Operators
boy = 18 girl = 68
public static void main(String[] args) { int age ; age = 3 ; Switch( age ) { case1 : System.out.println(“you can crawl”) ; break ; case2 : System.out.println(“you can talk”) ; break ; case3 : System.out.println(“you can get in trouble”) ; break ; default : System.out.println(“I don’t know how old are you”) }
010101010101010101010101
you can get in trouble
Logical Operators
age= 3
Assignment Operators
Operator Descrip-on Example
= Simple assignment operator, Assigns values from right side operands to le? side operand
C = A + B will assign value of A + B into C
+= Add AND assignment operator, It adds right operand to the le? operand and assign the result to le? operand
C += A is equivalent to C = C + A
-‐= Subtract AND assignment operator, It subtracts right operand from the le? operand and assign the result to le? operand
C -‐= A is equivalent to C = C -‐ A
*= Mul-ply AND assignment operator, It mul-plies right operand with the le? operand and assign the result to le? operand
C *= A is equivalent to C = C * A
/= Divide AND assignment operator, It divides le? operand with the right operand and assign the result to le? operand
C /= A is equivalent to C = C / A
%= Modulus AND assignment operator, It takes modulus using two operands and assign the result to le? operand
C %= A is equivalent to C = C % A
public static void main(String[] args) { int a , b , c ; a = 10 ; b = 20 ; c = 0 ; c = a + b ; System.out.println (c) ; }
010101010101010101010101
30
Assignment Operators
a = 10 b = 20 c = 0
public static void main(String[] args) { int a , b , c ; a = 10 ; b = 20 ; c = 0 ; c + = a ; System.out.println (c) ; }
010101010101010101010101
10
Assignment Operators
a = 10 b = 20 c = 0
public static void main(String[] args) { int a , b , c ; a = 10 ; b = 20 ; c = 0 ; c - = a ; System.out.println (c) ; }
010101010101010101010101
-10
Assignment Operators
a = 10 b = 20 c = 0
public static void main(String[] args) { int a , b , c ; a = 10 ; b = 20 ; c = 0 ; c*=a ; System.out.println (c) ; }
010101010101010101010101
0
Assignment Operators
a = 10 b = 20 c = 0
public static void main(String[] args) { int a , b , c ; a = 10 ; b = 20 ; c = 0 ; c/=a ; System.out.println (c) ; }
010101010101010101010101
0
Assignment Operators
a = 10 b = 20 c = 0
public static void main(String[] args) { int a , b , c ; a = 10 ; b = 20 ; c = 0 ; c%=a ; System.out.println (c) ; }
010101010101010101010101
0
Assignment Operators
a = 10 b = 20 c = 0
Assignment
أكتب برنامج يقوم بحساب مجموع الدرجات التالية : ٣٠ ، ٢٠ ، ٢٥. -
أكتب برنامج يقوم بمعرفة باقي قسمة : ٧/٢ . -
أكتب برنامج يقوم بمعرفة إذا كان الطالب ناجح او راسب . -
أكتب برنامج يقوم بطباعة قيمة ( -X . اوال ً ثم بعد ذلك تزويد هذه القيمة بــ ( ١) في الذاكرة (
أكتب برنامج يقوم بنقص قيمة ( -A) بـ (١) من الذاكرة ، ثم بعد ذلك طباعة قيمة (A . (
اكتب برنامج يقوم بطباعة ( -Excellent )وطباعة (٩٠ =< X ) إذا كان ( Very Good إذا(
) إذا Fail )إذا كان ( X >= ٥٠) وطباعـــــــــــــــة ( Goodكان ( X >= ٨٠ ) وطباعة (
.(٥٠ => X ) كان