Object Oriented Programming Lecture (2.1) Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com Department of Computer Science Najran University Ebtsam AbdelHakam 1
Outline 2 Operators Arithmetic Operators. Relational Operators. Boolean Logical Operators Assignment Operator The? Operator. Operator Precedence. Control Statements. If statement Nested ifs Ebtsam AbdelHakam
Arithmetic Operators Java operators can be divided into four groups: arithmetic, bitwise, relational, and logical. Ebtsam AbdelHakam 3
Arithmetic Operators Ebtsam AbdelHakam 4
Example 1 } } Ebtsam AbdelHakam 5
Example 1 Ebtsam AbdelHakam 6
Example 2 Ebtsam AbdelHakam 7
Prefix &Postfix operator 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) Ebtsam AbdelHakam
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 Ebtsam AbdelHakam 9
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 Ebtsam AbdelHakam 10
Another Example Prefix: int count=1; System.out.println(++count); displays 2. Postfix: int count=1; System.out.println(count++); displays 1. Ebtsam AbdelHakam 11
Relational Operators The relational operators determine the relationship that one operand has to the other. Ebtsam AbdelHakam 12
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. Ebtsam AbdelHakam 13
Boolean Logical Operators Ebtsam AbdelHakam 14
Example 4 Ebtsam AbdelHakam 15
Example 4 Ebtsam AbdelHakam 16
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. Ebtsam AbdelHakam 17
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. Ebtsam AbdelHakam 18
Example 5 Ebtsam AbdelHakam 19
Operator Precedence Table 1 shows the order of precedence for operators, from highest to lowest. Ebtsam AbdelHakam 20
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 Ebtsam AbdelHakam 21
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. Ebtsam AbdelHakam 22
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; Ebtsam AbdelHakam 23
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) Ebtsam AbdelHakam 24
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. Ebtsam AbdelHakam 25
Example 6 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); } } Ebtsam AbdelHakam 26