Skip to main content
Contents
Prev Up Next Scratch ActiveCode Profile
\(
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 5.9 Assessment: Sequential If-Statements
Subgoals for Evaluating Selection Statements.
Diagram which statements go together
For if statement, determine whether expression is true or false
If true – follow true branch, if false – follow else branch or do nothing if no else branch
Exercises Exercises
1.
Q1: A game has a series of three levels, but you only get the bonus points if you pass all three levels on the first try. Assume the following declarations:
boolean level1, level2, level3;
The variables indicate whether the level was passed on the first try (
true
indicates yes,
false
indicates no).
Which of the following code segments will properly update the
points
variable?
if (level1 && level2 && level3)
points += 5;
if (level1 || level2 || level3)
points += 5;
if (level1)
points += 5;
if (level2)
points += 5;
if (level3)
points += 5;
2.
Q2: A company offers a discount depending on the number of units of the product that are ordered:
Table 5.9.1.
1 up to but not including 10
10 up to but not including 50
Assume that the variables
numUnits, discount
have been declared and have values.
Which of the following code segments will work as intended?
if (numUnits >= 50)
discount = 0.2;
if (numUnits >= 10)
discount = 0.1;
if (numUnits > 0)
discount = 0;
if (numUnits >= 50)
discount = 0.2;
else if (numUnits >=10)
discount = 0.1;
else
discount = 0;
if (numUnits > 0)
discount = 0;
if (numUnits > 10)
discount = 0.1;
if (numUnits > 50)
discount = 0.2;
You have attempted
of
activities on this page.