Presentation is loading. Please wait.

Presentation is loading. Please wait.

Outline Relational Operators Boolean Operators Truth Tables Precedence Table Selection and Algorithms The if – else Variations of if The switch.

Similar presentations


Presentation on theme: "Outline Relational Operators Boolean Operators Truth Tables Precedence Table Selection and Algorithms The if – else Variations of if The switch."— Presentation transcript:

1 Outline Relational Operators Boolean Operators Truth Tables Precedence Table Selection and Algorithms The if – else Variations of if The switch

2 Relational Operators In addition to arithmetic operators which produce numeric results, relational operators are used in comparisons. OperatorMeaning ==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)

3 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

4 Truth Tables Let p and q be boolean expressions pqp && q true false truefalse pqp | | q true falsetrue falsetrue false p! p truefalse true

5 Precedence Table Operator TypeCode postfix expr++ expr-- unary ++expr --expr +expr -expr ! creation or cast new (type)expr Multiplicative * / % Additive + - relational = equality == != Logical AND && Logical OR || assignment = += -= *= /= %=

6 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 { }

7 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)

8 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”

9 The if – else The general structure is if (condition) statement1 else statement2 The condition is evaluated first. Statement1 is executed if result is true otherwise statement2 is executed.

10 The if – else condition statement1statement2 true false

11 Example public String getStatus( ) { String status; if (gpa >= 2) status = “good standing”; else status = “under probation”; return status; }

12 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

13 Variations of if (cont’d) condition statement true false

14 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

15 Nested IF (cont’d) condition1 statement1 true false condition2condition3conditionK statementK statement2 statement3 statementN true false...

16 Example The following example reads a day number [1..7] and prints the English name of the day. import java.io.*; class DayOfWeek { public static void main(String[] args) throws IOException { BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in)); System.out.print("Enter a number [1.. 7]: "); String input=stdin.readLine(); int dayNumber=Integer.parseInt(input); if (dayNumber == 1) System.out.println("Saturday"); else if (dayNumber == 2) System.out.println("Sunday"); else if (dayNumber == 3) System.out.println("Monday"); else if (dayNumber == 4) System.out.println("Tuesday"); else if (dayNumber == 5) System.out.println("Wednesday"); else if (dayNumber == 6) System.out.println("Thursday"); else if (dayNumber == 7) System.out.println("Friday"); else System.out.println("Wrong input"); }

17 The switch When selecting among many alternatives another construct, the switch may be used The general structure is: switch (controllingExpression) { case value1 : Statements; break; case value2 : Statements; break;... default : Statements; }

18 Example The following example reads a day number [1..7] and prints the English name of the day. import java.io.*; class DayOfWeek { public static void main(String[] args) throws IOException { BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in)); System.out.print("Enter a number [1.. 7]: "); String input=stdin.readLine(); int dayNumber=Integer.parseInt(input); switch (dayNumber) { 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"); }


Download ppt "Outline Relational Operators Boolean Operators Truth Tables Precedence Table Selection and Algorithms The if – else Variations of if The switch."

Similar presentations


Ads by Google