Title: Information Systems Programming with Java, 2nd edition
1Chapter 5Making Decisions
2Control Structures
You can control the flow of a program by using
one of the following three control structures
- Sequence (also known as straight line
programming) - Decision (also known as selection)
- Iteration (also known as repetition)
3Sequence Control Structure
Sequence is simply one instruction execution
after another in a sequential manner. The other
two, iteration and selection, allow for the flow
of a program to be altered depending on one or
more Boolean conditions.
4Iteration Control Structure
The iteration control structure allows for
repetition. It is implemented in Java using the
while, do/while, and for statements and will be
covered in the next chapter.
5Boolean Decisions
All computer decisions reduce to Boolean
decisions because of the digital nature of the
CPU. A Boolean decision is based on a Boolean
result of true or false. Boolean decisions are
made in Java using Boolean operators.
There are two types of Boolean operators used in
Java relational operators and logical operators.
6Java Relational Operators
Equal to ! Not equal to Greater than
Greater than or equal to
7Java Relational Operators
Relational operators can be combined with
arithmetic operators Example 5 3 What is the result of this operation?
false because 8 is not Relational operators are always performed last!
8Java Relational Operators
It is not a good idea to use the or !
operators to test floating-point values. Rather,
you should use , when testing
floating-point values. The reason is due to the
way that the computer represents floating point
values in memory. Unless you have infinite
precision, two floating-point values cannot,
theoretically, ever be equal.
9Java Logical Operators
! NOT OR AND
10Java Logical Operators
The NOT operator, !, is used to negate or invert
a Boolean value. !true is false and !false is
true
11Java Logical Operators
The OR operator, , is applied to multiple
Boolean values. If A and B are Boolean variables,
then the expression A B is true when either A
or B are true.
12Java Logical Operators
As you can see, any true results in true!
13Java Logical Operators
The AND operator, , also operates on multiple
Boolean values. If A and B are Boolean
variables, then the expression A B is true
only when both A and B are true.
14Java Logical Operators
As you can see, any false will result in false!
15Java Logical Operators
What value is generated as a result of the
following operation? (5!5) (33) false false
true is false Both conditions must evaluate to
true for an AND condition to be true
16String Comparisons
When comparing two string objects, you must use
the compareTo() method, not the Boolean
relational operators.
String myName Andy String yourName
Sandy myName.compareTo(yourName) Here, the
result would be negative, since the string Andy
is less than the string Sandy.
17Decision Control Structure
The decision control structure allows the program
flow to be altered depending upon conditions. It
is implemented in Java using the if, if/else, and
switch statements and is the topic of discussion
for this chapter.
18The Flow of the if Statement
The if statement is a decision control structure.
19if Statement Syntax
if(test expression) statement 1 statement
2 //COMPOUND STATEMENT
statement n //END IF
20Pseudocode
If balance .02 balance balance monthlyInterest Next
statement
21Java Code
if(balance monthlyInterest balance ? .02 balance
balance monthlyInterest //END if
- Note
- Test expression must be in ()
- are used for the statements
- Optional, but still recommended if theres only
one statement -
22Pseudocode
If (x Write x, y , sum Next statement
What is the implication of the AND in the if
statement?
If either test condition is false the entire
statement is false.
23Java Code
int x 2 int y 3 int sum
0 if((xSystem.out.println("x x "\ny
y "\nsum " sum) //END IF
What is the output?
24Output
The value of x is less than y and the value of y
is not equal to 10. As a result, the if
statements are executed, the two values are added
and the output is  x 2 y 3 sum 5
25if Statement
int x 3 int y 3 What happens when x
3? int sum 0 if((xy System.out.println("x "x"\ny "y
"\nsum " sum) //END IF
x is not less than y. Therefore, the two values
are not added and display would not occur.
26The Flow of the if/else Statement
The if/else statement allows for two sets of
statements.
27if/else Statement Syntax
if(test expression) statement 1
statement 2
statement n //END IF else statement
1 statement 2
statement n //END else
Notice that both the if statements and the else
statements are framed with braces.
Also, notice that indentation is used for clarity.
28What is wrong with the logic here?
If today is Friday Write Its PaydayWrite
Its not Payday
This will result in a logic error since it will
write both statements when today is Friday. How
do we fix it?
By constructing an If/Else statement, like this
29Pseudocode
If today is Friday Write Its
payday! Else Write Its not payday Write
Wow!
How would you actually code this in Java?
30Java Code
if(today.equals(Friday)) System.out.println(I
ts payday!) else System.out.println(Its
not payday) System.out.println(Wow)
Note that the equals() method must be used to
compare two strings for equality.
31When will the if statement be executed? When the
else statement will be executed?
if(x y
When x is less than y.
When x is greater than or equal to y.
32When will the if statement be executed? When the
else statement will be executed?
if(x ! 0) sum x y else difference x
? y
When x is nonzero.
When x is zero.
33Consider the following code
if((x y else difference x ? y
Notice the operator is used here!
The if is executed when the value of x is less
than the value of y and the value of 2x - y is
equal to zero.
The else is executed when the value of x is
greater than or equal to the value of y or
the value of 2x-y is not equal to zero.
34Consider the following code
if((x y else difference x ? y
Notice the operator is used here!
The if is executed when the value of x is less
than the value of y or the value of 2x-y is
equal to zero
The else is executed when the value of x is
greater than or equal to the value of y and the
value of 2x-y is not equal to zero
35Consider the following code
if(x 0) sum x y else difference x
? y
There is a syntax error here! Where?
The Boolean test is coded as x 0 and should be??
x 0
36Consider the following code
if(x 2?y) sum x y product x ?
y else difference x ? y
This wont compile! Why?
A dangling else!
How to fix it?
37if(x 2?y) sum x y product x ?
y else difference x ? y
You must frame multiple lines of an if statement
within braces to eliminate the dangling else!
The if is executed when the value of x is greater
than the value of 2y
The else will execute when the value of x is less
than or equal to the value of 2y
38Nested if/elses
Nothing more than an if/else statement within an
if/else statement.
39Consider a nested if/else to determine if a given
water temperature constitutes water, steam or ice.
If temperature 0 If temperature 100
Write STEAM Else Write
WATER Else Write ICE
Refer to the code in the text
40Convert this series ifs to nested if/elses.
if(year 1) System.out.println("Freshman") if
(year 2) System.out.println("Sophomore") if(
year 3) System.out.println("Junior") if(year
4) System.out.println("Senior") if(year
4) System.out.println("Graduate")
41If-else-If-else
if(year 4) System.out.println("Graduate"
) else if(year 3)
System.out.println("Senior") else
if(year 2)
System.out.println("Junior")
else if(year 1)
System.out.println("Sophomore")
else System.out.println("Freshman")
42If-If-else-else
if(year 1) if(year 2)
if(year 3)
if(year 4) System.out.println(
"Graduate") else
System.out.println("Senior") else
System.out.println("Junior")
else System.out.println("Sophomore")
else System.out.println("Freshman")
43The Switch statement
The switch statement allows the program to select
one of many options, also knows as cases.
The selection of a particular case is controlled
by a matching process where a selector variable
is compared against a series of case values.
44The flow of the Switch statement
45The Switch statement
If a match is found (the selector value case
value) the corresponding case statements are then
executed.
If no match is found, the program continues in a
straight line fashion with those statements
following the switch statement.
46Switch statement syntax
switch(selector variable) case case 1 value
case 1 statements
break case case 2 value case 2 statements
break
case case
n value case n statements //END SWITCH
47What happens if letterGrade is a B?
switch(letterGrade) case 'A'
io.writeInfo("Excellent") break
case 'B' io.writeInfo("Superior")
break case 'C' io.writeInfo("Average")
break case 'D'
io.writeInfo("Poor") break case
'F' io.writeInfo("Try Again") //END SWITCH
What if we leave out the break statements? And
A is selected?
48The Switch statement
Notice that the selector variable must follow the
keyword switch, within parentheses.
The selector variable must be an integral data
type ? character or integer. The selector
variable and case values must be the same data
type.
If you define a selector variable as a
floating-point or string object youll get a
compile error!
49The Switch statement
The switch block is comprised of several cases
identified by using the keyword case.
Note that a given case statement block can be any
number of statements and does not require
framing.
You must insert the keyword break as the last
statement in a given case statement block. If
you do not, any subsequent cases will be executed
after a given case match has occurred until a
break is encountered.
50The Switch statement
Be aware that there are times when you will want
to leave out the break statement. Consider the
situation where the selector variable could be
either a lower- or upper-case character value.
The break could follow the last of those two case
options, as follows
51switch(letterGrade) case 'a' case
'A' io.writeInfo("Excellent")
break case 'b' case 'B'
io.writeInfo("Superior") break
case 'c' case 'C'
io.writeInfo("Average") break
case 'd' case 'D'
io.writeInfo("Poor") break
case 'f' case 'F' io.writeInfo("Try
Again") //END SWITCH
52The default option
What would happen if there were no matches found
in the switch?
You create a default action should this situation
occur. If a match is not found the default is
executed.
The default option is an excellent tool to
protect against invalid entries from the users
when using the switch to produce menu driven
programs.
53switch(letterGrade) case 'a' case
'A' io.writeInfo("Excellent")
break case 'b' case 'B'
io.writeInfo("Superior") break
case 'c' case 'C'
io.writeInfo("Average") break
case 'd' case 'D'
io.writeInfo("Poor") break
case 'f' case 'F' io.writeInfo("Try
Again") break default
io.writeInfo("No match was found for the
entry letterGrade) //END
SWITCH
Adding a default
54Here, a default has been added to catch an
invalid letter grade. Notice that a break must
also be added to the last case. Why?