![Page 1: Object Oriented Programming Lecture (2.1) · 2014-02-09 · Ebtsam AbdelHakam 1 10/2/2014 Object Oriented Programming Lecture (2.1) Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1007e708231d432a9f7/html5/thumbnails/1.jpg)
10/2/2014 Ebtsam AbdelHakam 1
Object Oriented Programming Lecture (2.1)
Supervisor Ebtsam AbdelHakam
[email protected] Department of Computer Science
Najran University
![Page 2: Object Oriented Programming Lecture (2.1) · 2014-02-09 · Ebtsam AbdelHakam 1 10/2/2014 Object Oriented Programming Lecture (2.1) Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1007e708231d432a9f7/html5/thumbnails/2.jpg)
Outline
10/2/2014 Ebtsam AbdelHakam
2
● Operators Arithmetic Operators. Relational Operators. Boolean Logical Operators Assignment Operator The ? Operator. Operator Precedence.
● Control Statements. ● If statement ● Nested ifs
![Page 3: Object Oriented Programming Lecture (2.1) · 2014-02-09 · Ebtsam AbdelHakam 1 10/2/2014 Object Oriented Programming Lecture (2.1) Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1007e708231d432a9f7/html5/thumbnails/3.jpg)
10/2/2014 Ebtsam AbdelHakam 3
Arithmetic Operators
● Java operators can be divided into four groups: arithmetic, bitwise, relational, and logical.
![Page 4: Object Oriented Programming Lecture (2.1) · 2014-02-09 · Ebtsam AbdelHakam 1 10/2/2014 Object Oriented Programming Lecture (2.1) Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1007e708231d432a9f7/html5/thumbnails/4.jpg)
10/2/2014 Ebtsam AbdelHakam 4
Arithmetic Operators
![Page 5: Object Oriented Programming Lecture (2.1) · 2014-02-09 · Ebtsam AbdelHakam 1 10/2/2014 Object Oriented Programming Lecture (2.1) Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1007e708231d432a9f7/html5/thumbnails/5.jpg)
10/2/2014 Ebtsam AbdelHakam 5
Example 1
} }
![Page 6: Object Oriented Programming Lecture (2.1) · 2014-02-09 · Ebtsam AbdelHakam 1 10/2/2014 Object Oriented Programming Lecture (2.1) Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1007e708231d432a9f7/html5/thumbnails/6.jpg)
10/2/2014 Ebtsam AbdelHakam 6
Example 1
![Page 7: Object Oriented Programming Lecture (2.1) · 2014-02-09 · Ebtsam AbdelHakam 1 10/2/2014 Object Oriented Programming Lecture (2.1) Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1007e708231d432a9f7/html5/thumbnails/7.jpg)
10/2/2014 Ebtsam AbdelHakam 7
Example 2
![Page 8: Object Oriented Programming Lecture (2.1) · 2014-02-09 · Ebtsam AbdelHakam 1 10/2/2014 Object Oriented Programming Lecture (2.1) Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1007e708231d432a9f7/html5/thumbnails/8.jpg)
Prefix &Postfix operator
10/2/2014 Ebtsam AbdelHakam
8
• The increment operators used to adds 1 to previous value. Increment operator is unary operator because only one variable is
used. There are 2 type in increment/decrement operator.
1. Postfix increment operator (variable++) 2. Prefix increment operator (++variable)
Both prefix and postfix increment operator are same thing when they used independently(i++ or ++i) ie adds 1 to previous value.
But they behave differently when they used in expression on the right hand side of assignment statement(num=i++ or num=++i)
![Page 9: Object Oriented Programming Lecture (2.1) · 2014-02-09 · Ebtsam AbdelHakam 1 10/2/2014 Object Oriented Programming Lecture (2.1) Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1007e708231d432a9f7/html5/thumbnails/9.jpg)
10/2/2014 Ebtsam AbdelHakam 9
Postfix Example
class group{ public static void main(String arng[]){ int num, i; i=0; i++; num=i++; System.out.println("i="+i); System.out.println("num="+num); } } Output:- i=2 num=1
![Page 10: Object Oriented Programming Lecture (2.1) · 2014-02-09 · Ebtsam AbdelHakam 1 10/2/2014 Object Oriented Programming Lecture (2.1) Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1007e708231d432a9f7/html5/thumbnails/10.jpg)
10/2/2014 Ebtsam AbdelHakam 10
Prefix Example
class group{ public static void main(String arng[]){ int num, i; i=0; ++i; num=++i; System.out.println("i="+i); System.out.println("num="+num); } } Output:- i=2 num=2
![Page 11: Object Oriented Programming Lecture (2.1) · 2014-02-09 · Ebtsam AbdelHakam 1 10/2/2014 Object Oriented Programming Lecture (2.1) Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1007e708231d432a9f7/html5/thumbnails/11.jpg)
10/2/2014 Ebtsam AbdelHakam 11
Another Example
Prefix: int count=1; System.out.println(++count); displays 2.
Postfix: int count=1; System.out.println(count++); displays 1.
![Page 12: Object Oriented Programming Lecture (2.1) · 2014-02-09 · Ebtsam AbdelHakam 1 10/2/2014 Object Oriented Programming Lecture (2.1) Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1007e708231d432a9f7/html5/thumbnails/12.jpg)
10/2/2014 Ebtsam AbdelHakam 12
Relational Operators
The relational operators determine the relationship that one operand has to the other.
![Page 13: Object Oriented Programming Lecture (2.1) · 2014-02-09 · Ebtsam AbdelHakam 1 10/2/2014 Object Oriented Programming Lecture (2.1) Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1007e708231d432a9f7/html5/thumbnails/13.jpg)
10/2/2014 Ebtsam AbdelHakam 13
Boolean Logical Operators
The Boolean logical operators operate only on boolean
operands. All of the binary logical operators combine two boolean
values to form a resultant boolean value.
![Page 14: Object Oriented Programming Lecture (2.1) · 2014-02-09 · Ebtsam AbdelHakam 1 10/2/2014 Object Oriented Programming Lecture (2.1) Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1007e708231d432a9f7/html5/thumbnails/14.jpg)
10/2/2014 Ebtsam AbdelHakam 14
Boolean Logical Operators
![Page 15: Object Oriented Programming Lecture (2.1) · 2014-02-09 · Ebtsam AbdelHakam 1 10/2/2014 Object Oriented Programming Lecture (2.1) Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1007e708231d432a9f7/html5/thumbnails/15.jpg)
10/2/2014 Ebtsam AbdelHakam 15
Example 4
![Page 16: Object Oriented Programming Lecture (2.1) · 2014-02-09 · Ebtsam AbdelHakam 1 10/2/2014 Object Oriented Programming Lecture (2.1) Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1007e708231d432a9f7/html5/thumbnails/16.jpg)
10/2/2014 Ebtsam AbdelHakam 16
Example 4
![Page 17: Object Oriented Programming Lecture (2.1) · 2014-02-09 · Ebtsam AbdelHakam 1 10/2/2014 Object Oriented Programming Lecture (2.1) Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1007e708231d432a9f7/html5/thumbnails/17.jpg)
10/2/2014 Ebtsam AbdelHakam 17
The Assignment Operator The assignment operator is the single equal sign, =. It has this general form: var = expression; The type of var must be compatible with the type of expression. It allows you to create a chain of assignments. For example, consider this fragment: int x, y, z; x = y = z = 100; // set x, y, and z to 100 This fragment sets the variables x, y, and z to 100 using a single
statement.
![Page 18: Object Oriented Programming Lecture (2.1) · 2014-02-09 · Ebtsam AbdelHakam 1 10/2/2014 Object Oriented Programming Lecture (2.1) Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1007e708231d432a9f7/html5/thumbnails/18.jpg)
10/2/2014 Ebtsam AbdelHakam 18
The ? Operator
● Java includes a special ternary (three-way) operator that can replace certain types of if-then-else statements.
● The ? has this general form: expression1 ? expression2 : expression3 ● Here, expression1 can be any expression that evaluates to a boolean
value (condition). ● If expression1 is true, then expression2 is evaluated; otherwise,
expression3 is evaluated. ● The result of the ? operation is that of the expression evaluated.
![Page 19: Object Oriented Programming Lecture (2.1) · 2014-02-09 · Ebtsam AbdelHakam 1 10/2/2014 Object Oriented Programming Lecture (2.1) Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1007e708231d432a9f7/html5/thumbnails/19.jpg)
10/2/2014 Ebtsam AbdelHakam 19
Example 5
![Page 20: Object Oriented Programming Lecture (2.1) · 2014-02-09 · Ebtsam AbdelHakam 1 10/2/2014 Object Oriented Programming Lecture (2.1) Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1007e708231d432a9f7/html5/thumbnails/20.jpg)
10/2/2014 Ebtsam AbdelHakam 20
Operator Precedence ● Table 1 shows the order of precedence for operators, from highest to lowest.
![Page 21: Object Oriented Programming Lecture (2.1) · 2014-02-09 · Ebtsam AbdelHakam 1 10/2/2014 Object Oriented Programming Lecture (2.1) Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1007e708231d432a9f7/html5/thumbnails/21.jpg)
10/2/2014 Ebtsam AbdelHakam 21
Operator precedence
● Evaluate the following expressions:
● 3+5/ 7 answer: 3
● 3*3 +3%2 answer: 10
● 3+2/5+ -2*4 answer: -5
● 3+(2/5)+ (-2*4) answer: -5
● 3*(3+3)%2 answer: 0
![Page 22: Object Oriented Programming Lecture (2.1) · 2014-02-09 · Ebtsam AbdelHakam 1 10/2/2014 Object Oriented Programming Lecture (2.1) Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1007e708231d432a9f7/html5/thumbnails/22.jpg)
10/2/2014 Ebtsam AbdelHakam 22
Control Statments ● Control statements are used to cause the flow of execution to
advance and branch based on changes to the state of a program. ● Control Statments include:
● Selection statements allow your program to choose different paths of execution based upon the outcome of an expression or the state of a variable.
● Iteration statements enable program execution to repeat one or more statements (loops).
● Jump statements allow your program to execute in a nonlinear fashion.
![Page 23: Object Oriented Programming Lecture (2.1) · 2014-02-09 · Ebtsam AbdelHakam 1 10/2/2014 Object Oriented Programming Lecture (2.1) Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1007e708231d432a9f7/html5/thumbnails/23.jpg)
10/2/2014 Ebtsam AbdelHakam 23
Java’s Selection Statements ● Java supports two selection statements: if and switch. if (condition) statement1; else statement2;
● For example:
int a, b;
if(a < b) a = 0;
else b = 0;
![Page 24: Object Oriented Programming Lecture (2.1) · 2014-02-09 · Ebtsam AbdelHakam 1 10/2/2014 Object Oriented Programming Lecture (2.1) Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1007e708231d432a9f7/html5/thumbnails/24.jpg)
10/2/2014 Ebtsam AbdelHakam 24
Nested ifs ● When you nest ifs, remember that an else statement always refers
to the nearest if statement that is within the same block as the else and that is not already associated with an else.
if(i == 10)
{ if(j < 20) a = b;
if(k > 100) c = d; // this if is
else a = c; // astsociated with this else
}
else a = d; // this else refers to if(i == 10)
![Page 25: Object Oriented Programming Lecture (2.1) · 2014-02-09 · Ebtsam AbdelHakam 1 10/2/2014 Object Oriented Programming Lecture (2.1) Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1007e708231d432a9f7/html5/thumbnails/25.jpg)
10/2/2014 Ebtsam AbdelHakam 25
The if-else-if Ladder if(condition)
statement;
else if(condition)
statement;
else if(condition)
statement;
...
else
statement; ● The if statements are executed from the top down. As soon as one of the
conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.
![Page 26: Object Oriented Programming Lecture (2.1) · 2014-02-09 · Ebtsam AbdelHakam 1 10/2/2014 Object Oriented Programming Lecture (2.1) Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com](https://reader033.vdocuments.mx/reader033/viewer/2022060315/5f0be1007e708231d432a9f7/html5/thumbnails/26.jpg)
10/2/2014 Ebtsam AbdelHakam 26
Program that checks number is zero, positive or negative using if-else-if ladder
class CheckSignNumberDemo { public static void main(String args[]) {
int x = 10; if(x <= -1) System.out.println("x is a negative number having value " + x); else if(x == 0) System.out.println("x is a zero number having value " + x); else if(x > 0) System.out.println("x is a positive number having value " + x);
} }
Example 6