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
ExercisesExercises
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;
I only
II only
III only
I and III
II and III
2.
Q2: A company offers a discount depending on the number of units of the product that are ordered:
Table5.9.1.
Number of Units
Discount
1 up to but not including 10
0
10 up to but not including 50
10%
50 or more
20%
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;