Transcript Lecture 14

Selection Statements
Overview

Relational and Logical Operations

Selection structures
» if statement
» if-else statement

Preview: More on Selection Statements
1
Relational Operators
In addition to Arithmetical computations,
problems often require comparison operations.
 For example, to find whether a student has passed
ICS102, we compare the student score with the
pass mark.
 For comparison operations, java provides a set of
operators called relational operators.
 Expressions involving relational operators are
called relational expressions.
 A relational expression will result in either true or
false, with regard to the operand values
 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
2
Relational Operators
Operator
Use
>
op1
>
>=
op1
>=
<
op1
<
<=
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
If (x
>
==
op2
Return true if
op2
op2
b)
y)
op1 is greater than op2
op1 is not
less than op2
op1 is less than op2
statement1
statement2
3
Logical Operators







In reality, to pass ICS102, a pass in both the
lecture and lab. components is required.
Suppose that a pass mark in the lecture component
is 50 out of 75
And a pass mark in the Lab component is 15 out of
25
Now the student mark has 2 components:
lectureMark = 64
labMark = 21
Here we have to check for both pass in lecture
mark and pass in lab:
lectureMark >= 50
labMark >= 15
If both expressions are true, the student has passed
the course.
To combine two such conditions , we use Logical
operators as follows:
classMark >= 50 && labMark >= 15
4
Logical Operators
Where && is called AND operator which results in
true if both the sides of it are true.
Operator
&&
Use
Returns true if
op1 && op2 op1 and op2 are both true
||
op1 || op2
!
! op1
either op1 or op2 is true
op1 is false
For && operator :
Opr 1
Opr 2
Result
True
True
True
True
False
False
False
True
False
False
False
False
5
Logical Operators
For || operator :
Opr 1
Opr 2
Result
True
True
True
True
False
True
False
True
True
False
False
False
For ! Operator :
! Opr
Result
True
False
False
True
6
Operator precedence


Now that we have seen additional Operators, we
need to revisit the precedence table to see how
they all relate to each other.
The following table shows precedence from the
highest to the lowest:
expr++ expr-postfix operators
unary operators ++expr --expr +expr
-expr !
creation or cast new (type)expr
* / %
Multiplicative
Additive
relational
equality
Logical AND
Logical OR
+ -
Conditional
assignment
?:
< > <= >=
== !=
&&
||
=
+=
-=
*=
/=
%=
7
if statement syntax





The programs we have written so far have all been
sequential. That is, all the statements are executed
from beginning to end.
How ever, most real life programs require some
form of selection.
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!
The if Statement allows us to test for some
condition and then select which part of a program
to execute.
The simplest form of the if statement has the
syntax:
if (condition)
statement

Interpretation
» If the condition is true, the statement
is executed; if condition is false, the
statement is skipped
8
if statement Flow diagram

Flow diagram for if statement
condition
false
true
statement

The withdraw method can thus be modified as:
public void withdraw(double amount) {
if (amount <= balance)
balance = balance – amount;
}
}
9
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");
}
}
10
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.
11
if else statement syntax

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
12
If else statement Flow diagram
Flow diagram for if
condition
else statement
false
true
statement1
statement2
13
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“);
}
}
14