Outline Relational Operators Boolean Operators Truth Tables Precedence
Outline • • Relational Operators Boolean Operators Truth Tables Precedence Table Selection and Algorithms The if – else Variations of if The switch
Relational Operators • In addition to arithmetic operators which produce numeric results, relational operators are used in comparisons. Operator Meaning == equal != not equal > greater than >= greater than or equal < less than <= less than or equal The result of a comparison is boolean (i. e. , true or false)
Boolean Operators • Connecting relational expressions requires another set of operators called boolean operators Operator && || ! Meaning logical and logical or negation • Example: grade >= 0 && grade <= 100. 0
Truth Tables • Let p and q be boolean expressions p !p true false true p q p && q true false true false p q p||q true true false false
Precedence Table Operator Type postfix Code expr++ expr-- unary ++expr --expr +expr creation or cast new (type)expr Multiplicative * / % Additive + - relational < equality == != Logical AND && Logical OR || assignment = > += <= -= >= *= /= %= -expr !
Statements • Statements can be simple or compound • A number of statements can be grouped together to form one compound statement by enclosing these statements in curly brackets { }
Selection and Algorithms • We have seen that flow control has three types: – Sequential – Selection – Iteration • In algorithm flow, we select one of the alternatives based on a criteria (condition)
Selection and Algorithms • Assume that we want to find the status of a student given his GPA • Algorithm – Get GPA – if GPA >= 2 • status = “good standing” – else • status=“under probation”
The if – else • The general structure is if (condition) statement 1 else statement 2 • The condition is evaluated first. Statement 1 is executed if result is true otherwise statement 2 is executed.
The if – else true statement 1 condition false statement 2
Example public String get. Status( ) { String status; if (gpa >= 2) status = “good standing”; else status = “under probation”; return status; }
Variations of if • The else part in the if may be omitted if nothing need to be done when the condition is false • In such case, the general structure is if (condition) statement
Variations of if (cont’d) true statement condition false
Nested IF • The else part of an if-statement may contain another if-statement to provide multiple selection • The general structure is if (condition 1) statement 1 else if(condition 2) statement 2 …… else statement n
Nested IF (cont’d) statement 1 true condition 1 false statement 2 true condition 2 false statement 3 true condition 3 false . . . statement. K true condition. K false statement. N
Example • The following example reads a day number [1. . 7] and prints the English name of the day. import java. io. *; class Day. Of. Week { public static void main(String[] args) throws IOException { Buffered. Reader stdin = new Buffered. Reader( new Input. Stream. Reader(System. in)); System. out. print("Enter a number [1. . 7]: "); String input=stdin. read. Line(); int day. Number=Integer. parse. Int(input); if (day. Number == 1) System. out. println("Saturday"); else if (day. Number == 2) System. out. println("Sunday"); else if (day. Number == 3) System. out. println("Monday"); else if (day. Number == 4) System. out. println("Tuesday"); else if (day. Number == 5) System. out. println("Wednesday"); else if (day. Number == 6) System. out. println("Thursday"); else if (day. Number == 7) System. out. println("Friday"); else System. out. println("Wrong input"); } }
The switch • When selecting among many alternatives another construct, the switch may be used • The general structure is: switch (controlling. Expression) { case value 1 : Statements; break; case value 2 : Statements; break; . . . default : Statements; }
Example • The following example reads a day number [1. . 7] and prints the English name of the day. import java. io. *; class Day. Of. Week { public static void main(String[] args) throws IOException { Buffered. Reader stdin = new Buffered. Reader( new Input. Stream. Reader(System. in)); System. out. print("Enter a number [1. . 7]: "); String input=stdin. read. Line(); int day. Number=Integer. parse. Int(input); switch (day. Number) { case 1: System. out. println("Saturday"); break; case 2: System. out. println("Sunday"); break; case 3: System. out. println("Monday"); break; case 4: System. out. println("Tuesday"); break; case 5: System. out. println("Wednesday"); break; case 6: System. out. println("Thursday"); break; case 7: System. out. println("Friday"); break; default: System. out. println("Wrong input"); } } }
- Slides: 18