Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Selection Statements Overview l Relational and Logical Operations l Selection structures »if statement »if-else statement l Preview: More on Selection.

Similar presentations


Presentation on theme: "1 Selection Statements Overview l Relational and Logical Operations l Selection structures »if statement »if-else statement l Preview: More on Selection."— Presentation transcript:

1 1 Selection Statements Overview l Relational and Logical Operations l Selection structures »if statement »if-else statement l Preview: More on Selection Statements

2 2 Relational Operators l In addition to Arithmetical computations, problems often require comparison operations. l For example, to find whether a student has passed ICS102, we compare the student score with the pass mark. l For comparison operations, java provides a set of operators called relational operators. l Expressions involving relational operators are called relational expressions. l A relational expression will result in either true or false, with regard to the operand values l Example : Given that studentMark = 80 passMark = 65 To check for pass, the relational expression is : studentMark >= passMark Here >= is a relational operator. This expression will result in true, for those values of studentMark which are greater than or equal to the value of passMark

3 3 Relational Operators OperatorUseReturn true if > op1 > op2 op1 is greater than op2 >= op1 >= op2op1 is not less than o p2 < op1 < op2 op1 is less than op2 <= op1 <= op2 op1 is less than or equal to op2 == op1 == op2 op1 and op2 are equal != op1 != op2 op1 and op2 are not equal Examples : if (a > b)statement1 If (x == y) statement2

4 4 Logical Operators l In reality, to pass ICS102, a pass in both the lecture and lab. components is required. l Suppose that a pass mark in the lecture component is 50 out of 75 l And a pass mark in the Lab component is 15 out of 25 l Now the student mark has 2 components: lectureMark = 64 labMark = 21 l Here we have to check for both pass in lecture mark and pass in lab: lectureMark >= 50 labMark >= 15 l If both expressions are true, the student has passed the course. l To combine two such conditions, we use Logical operators as follows: classMark >= 50 && labMark >= 15

5 5 Logical Operators Where && is called AND operator which results in true if both the sides of it are true. OperatorUseReturns true if && op1 && op2op1 and op2 are both true || op1 || op2 either op1 or op2 is true ! ! op1 op1 is false For && operator : Opr 1Opr 2Result True False TrueFalse

6 6 Logical Operators For || operator : For ! Operator : Opr 1Opr 2Result True FalseTrue FalseTrue False ! OprResult TrueFalse True

7 7 Operator precedence l Now that we have seen additional Operators, we need to revisit the precedence table to see how they all relate to each other. l The following table shows precedence from the highest to the lowest: postfix operators expr++ expr-- unary operators ++expr --expr +expr -expr ! creation or cast new (type)expr Multiplicative * / % Additive + - relational = equality == != Logical AND && Logical OR || Conditional ?: assignment = += -= *= /= %=

8 8 if statement syntax l The programs we have written so far have all been sequential. That is, all the statements are executed from beginning to end. l How ever, most real life programs require some form of selection. l For example, the withdraw method of the bank account example is clearly not realistic – it allows the customer to withdraw as much as he likes! l The if Statement allows us to test for some condition and then select which part of a program to execute. l The simplest form of the if statement has the syntax: if (condition) statement l Interpretation »If the condition is true, the statement is executed; if condition is false, the statement is skipped

9 9 if statement Flow diagram l Flow diagram for if statement l The withdraw method can thus be modified as: public void withdraw(double amount) { if (amount <= balance) balance = balance – amount; } statement condition false true

10 10 if statement Example import java.io.InputStreamReader; import java.io.BufferedReader; class IfExample { public static void main(String[]args) throws java.io.IOException { int number; BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ) ); System.out.print("Enter a number: "); String input = stdin.readLine(); number= Integer.parseInt(input); if (number % 2 == 0 ) System.out.println("The number is even"); }

11 11 if statement Example In the previous example, what will happen if the given number is not an even number? The answer is, the message will not be printed. But it would be nice to print, The Number is not an Even To have this alternate decision, Java has another type of if statement with else clause.

12 12 if else statement syntax l Syntax »An else clause can be added to an if statement to make it an if-else statement: if (condition) statement1 else statement2 Interpretation »If the condition is true, statement1 is executed; if the condition is false, statement2 is executed

13 13 If else statement Flow diagram Flow diagram for if else statement statement1 condition false true statement2

14 14 If else statement Example import java.io.InputStreamReader; import java.io.BufferedReader; class IfElseExample { public static void main(String[]args) throws java.io.IOException { int number; BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ) ); System.out.print("Enter a number: "); String input = stdin.readLine(); number= Integer.parseInt(input); if (number % 2 == 0 ) System.out.println("The number is even"); else System.out.println("The number is not even“); }


Download ppt "1 Selection Statements Overview l Relational and Logical Operations l Selection structures »if statement »if-else statement l Preview: More on Selection."

Similar presentations


Ads by Google