Lecture 11: Selection

Download Report

Transcript Lecture 11: Selection

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
false
true
p
q
p && q
true
true
true
true
false
false
false
true
false
false
false
false
p
q
p||q
true
true
true
true
false
true
false
true
true
false
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)
statement1
else
statement2
• The condition is evaluated first. Statement1
is executed if result is true otherwise
statement2 is executed.
The if – else
true
statement1
condition
false
statement2
Example
public String getStatus( )
{
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)
statement1
true
condition1
false
statement2
true
condition2
false
statement3
true
condition3
false
. . .
statementK
true
conditionK
false
statementN
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");
}
}
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;
}
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");
}
}
}