Java Programming,
10e
Chapter 05: Making Decisions
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
1
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Chapter Objectives
By the end of this chapter, you should be able to:
05.01 Plan decision-making logic
05.02 Make decisions with the if and if…else statements
05.03 Use multiple statements in if and if…else clauses
05.04 Nest if and if…else statements
05.05 Use AND and OR operators
05.06 Make accurate and efficient decisions
05.07 Use switch
05.08 Use the conditional and NOT operators
05.09 Assess operator precedence
05.10 Make constructors more efficient by using decisions in other methods
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.1 Planning Decision-Making Logic (1 of 5)
Pseudocode
• Use paper and a pencil
• Plan a program’s logic by writing plain English statements
• Accomplish important steps in a given task
• Use everyday language
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.1 Planning Decision-Making Logic (2 of 5)
Flowchart
• Steps in diagram form
• A series of shapes connected by arrows
• Programmers use a variety of shapes for different tasks
• Rectangle to represent any unconditional step
• Diamond to represent any decision
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.1 Planning Decision-Making Logic (3 of 5)
Sequence structure
• One step follows another unconditionally
• Cannot branch away or skip a step
Decision structure
• Involves choosing among alternative courses of action
• Based on some value within a program
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.1 Planning Decision-Making Logic (4 of 5)
Figure 5-1:
Flowchart of
a series of
sequential Figure 5-2: Flowchart
steps including a decision
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.1 Planning Decision-Making Logic (5 of 5)
All computer decisions are yes-or-no decisions
Boolean values
• true and false values
• Used in every computer decision
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.2 The if and if…else Statements (1 of 6)
if statement
• Sometimes called a single-alternative decision
• The simplest statement to make a decision
• A Boolean expression appears within parentheses
• No space between the keyword if and the opening parenthesis
• Execution always continues to the next independent statement
• Use a double equal sign ( == ) to determine equivalency
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.2 The if and if…else Statements (2 of 6)
Figure 5-3: A Java if statement
and its logic
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.2 The if and if…else Statements (3 of 6)
Pitfall: misplacing a semicolon in an if statement
• There should be no semicolon at the end of the first line of the if statement
• The statement does not end there
• An empty statement contains only a semicolon
• Execution continues with the next independent statement
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.2 The if and if…else Statements (4 of 6)
Pitfall: using the assignment operator instead of the equivalency operator
• A single equal sign instead of a double
• Instead of comparing, it attempts to assign a value
• Illegal because only Boolean expressions are allowed
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.2 The if and if…else Statements (5 of 6)
Pitfall: attempting to compare objects using the relational operators
• Use standard relational operators to compare values of primitive data types
• Cannot use them to compare objects because the program won’t compile
• To compare values of objects, write specialized methods
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.2 The if and if…else Statements (6 of 6)
The if…else statement
• Used for a dual-alternative selection
• You can code an if without an else, but it is illegal to code an else
without an if
• Only one of the resulting actions takes place depending on the evaluation of
the Boolean expression
• Each statement ends with a semicolon
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 5.1: Knowledge Check
An if statement is sometimes called a(n) ___________-alternative selection.
a. double
b. single
c. individual
d. conditional
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 5.1: Knowledge Check Answer
An if statement is sometimes called a(n) ___________-alternative selection.
Answer: b. single
An if statement is sometimes called a single-alternative selection because
there is only one alternative—the true alternative.
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.3 Using Multiple Statements in if and if…else
Clauses (1 of 2)
You can take more than one action following the evaluation of a Boolean
expression within an if statement
• For example, to display several lines of output or perform several
mathematical calculations
• Use curly braces correctly
• If you fail to block statements that should depend on an if, and you also
use an else clause, the program will not compile
• You can also block statements to depend on an else
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.3 Using Multiple Statements in if and if…else
Clauses (2 of 2)
Figure 5-10: Two typical executions of
the Payroll application
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.4 Nesting if and if…else Statements (1 of 1)
Nested if statements
• Statements in which an if structure is contained inside another if structure
• Two conditions must be met before some action is taken
• Pay careful attention to the placement of else clauses
• else statements are always associated with if on a “first in-last out” basis
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.5 Using Logical AND and OR Operators (1 of 3)
Compound Boolean expression
• Also called compound condition
• Combine Boolean tests into a single expression using AND and OR
AND Operator
• &&
• Alternative to some nested if statements
• true when both of its operands are true
• Must include a complete Boolean expression on each side of &&
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.5 Using Logical AND and OR Operators (2 of 3)
OR operator
• ||
• Alternative to nested if statements
• true when at least one of its operands are true
• Makes your code more concise, less error-prone, and easier to understand
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.5 Using Logical AND and OR Operators (3 of 3)
Short-circuit evaluation
• The expressions on each side of the && and || operators are evaluated only
as far as necessary to determine whether the entire expression is true or
false
• With the && operator, if the first expression is false, the second is never
evaluated because it doesn’t matter
• With the || operator, if the first expression is true, the second is never
evaluated, but if the first is false, it is
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 5.2: Discussion
1. Explain why programmers use pseudocode, flowcharts, and decision
structures. What are each used for?
2. Differentiate between nesting if statements and using the AND operator.
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 5.2: Discussion Debrief
1. Programmers use pseudocode, flowcharts, and decision structures to plan
the decision-making logic before they start typing code. Pseudocode uses
plain English to describe the steps needed to accomplish a task. A flowchart
is similar to pseudocode, but the steps are written in diagram form. A
decision structure is used to outline choices between alternative courses of
action based on some value within a program.
2. A nested if statement is one in which a decision is contained inside either
if or else clauses of another decision. The logical AND operator, &&, is
an alternative to a nested if statement. The && operator is used between
two Boolean expressions to create a compound Boolean expression that is
true when both of its operands are true. Using either method achieves
the same result, but using the && operator often makes your code more
concise, less error-prone, and easier to understand.
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.6 Making Accurate and Efficient Decisions (1 of 1)
Range check
• A series of if statements that determine whether a value falls within a
specified range
• It is most efficient to first ask the question that is most likely to be true
• Beginning programmers often && or || when they mean to use the other
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.7 Using switch (1 of 3)
switch statement
• Alternative to a series of nested if statements
• Useful when you need to test a single variable against a series of exact
integer, character, or string values
• Uses four keywords:
• switch (start of statement, followed by a test expression in parentheses)
• case (followed by one of the possible values for the test expression and a colon)
• break (optionally terminates a switch statement at the end of each case)
• default (optionally used prior to any action that should occur if the test variable
does not match any case)
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.7 Using switch (2 of 3)
Figure 5-25: Determining all the tasks left for the week using
a switch statement
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.7 Using switch (3 of 3)
switch expression
• Java 14 or later
• When all cases result in an assignment to the same variable
• Multiple case labels are separated by commas
• The arrow operator (->) is used instead of the assignment operator to
assign a value
• Each case does not have to end with the keyword break
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.8 Using the Conditional and NOT Operators (1 of 3)
Conditional operator
• Requires three expressions (ternary operator) separated with a question
mark and colon
• An abbreviation of the if…else statement
• You are never required to use the conditional operator
• The disadvantage is that it is more difficult to read
• The advantage is the conciseness of the statement
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.8 Using the Conditional and NOT Operators (2 of 3)
NOT operator
• !
• To negate the result of any Boolean expression
• Any expression that evaluates as true becomes false, and the reverse
• Harder to read because of double parentheses
• Can be clearer when the value of a Boolean variable is tested
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.8 Using the Conditional and NOT Operators (3 of 3)
Figure 5-33: Four if…else statements that all do
the same thing
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 5.3: Discussion
1. What happens if you fail to block statements that should depend on an if,
and you also use an else clause? What are curly braces used for?
2. For what purpose is a switch statement used? What do each of the four
switch statement keywords do?
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 5.3: Discussion Debrief
1. When you fail to block statements that should depend on an if, and you
also use an else clause, the program will not compile. The error message
will indicate that the program contains else without if. Curly braces are used
to execute more than one statement that depends on the evaluation of a
Boolean expression to place the dependent statements in a block.
2. A switch statement is used to test a single variable against a series of
exact integer, character, or string values. switch starts the statement and
is followed immediately by a test expression enclosed in parentheses. case
is followed by one of the possible values for the test expression and a
colon. break optionally terminates a switch statement at the end of each
case. default optionally is used prior to any action that should occur if the
test variable does not match any case.
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.9 Understanding Operator Precedence (1 of 2)
Operator precedence
• You can combine as many && or || operators in an expression as you need
to make a decision
• An operator’s precedence makes a difference in how an expression is
evaluated
• You can use parentheses to change precedence or make intentions clear
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.9 Understanding Operator Precedence (2 of 2)
Order of precedence from highest to lowest
• Logical NOT operator
• Multiplication, division, modulus
• Addition, subtraction
• Relational
• Equality
• Logical AND
• Logical OR
• Conditional
• Assignment
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
5.10 Making Constructors More Efficient by Using
Decisions in Other Methods (1 of 1)
Control values assigned to fields
• You often will need to restrict values assigned to fields
• One way is to have the rules governing appropriate values be the same as
those used in the constructor by coding the decisions once in the set
methods and having the constructors use the set method logic
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 5.4: Think, Pair, Share
1. Form pairs or groups of 2 to 4 class members.
2. Decide on an application that you will create that uses multiple statements
and includes the conditional and NOT operators. What will you do, and what
steps should you take?
3. You want to do a range check. What will this help you do?
4. Explain how you will ensure that operator precedence is followed.
5. How can you use a decision in another method to make your constructors
more efficient?
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 5.4: Think, Pair, Share Debrief
• How did you use the conditional and NOT operators? What steps did you
take?
• What conclusions did you reach with the range check? How did this help
your program?
• What steps did you take to ensure operator precedence was correctly
followed? What is the order of precedence?
• Which areas of your program might be improved by using a decision in
another method? Why would this help?
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Don’t Do It
• Don’t ignore subtleties in boundaries used in decision making.
• Don’t use the assignment operator instead of the comparison operator when testing for
equality.
• Don’t insert a semicolon after the Boolean expression in an if statement; insert the
semicolon after the entire statement is completed.
• Don’t forget to block a set of statements with curly braces when several statements
depend on the if or the else clause.
• Don’t forget to include a complete Boolean expression on each side of an && or ||
operator in a compound Boolean expression.
• Don’t try to use a switch statement to test anything other than a byte, short, char,
int, or String.
• Don’t forget a break statement if one is required by the logic of your switch
statement.
• Don’t use the standard relational operators to compare objects; use them only with the
built-in Java types.
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Self-Assessment
1. What is the difference between pseudocode and a flowchart?
2. What might happen if you misplace a semicolon in an if statement?
3. What techniques can you use to make sure that multiple statements and/or
operators are used correctly?
4. Differentiate between the AND, OR, conditional, and NOT operators.
5. For what purpose would you use a range check?
6. List the four keywords a switch statement might use.
7. How might you make constructors more efficient?
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Summary
Click the link to review the objectives for this presentation.
Link to Objectives
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.