5.1 Fill in the blanks in each of the following statements:
Typically, statements are used for counter-controlled iteration and statements for sentinel-controlled iteration.
The do
…while
statement tests the loop-continuation condition executing the loop’s body; therefore, the body always executes at least once.
The statement selects among multiple actions based on the possible values of an integer variable or expression.
The statement, when executed in an iteration statement, skips the remaining statements in the loop body and proceeds with the next iteration of the loop.
The operator can be used to ensure that two conditions are both true before choosing a certain path of execution.
If the loop-continuation condition in a for
header is initially , the program does not execute the for
statement’s body.
5.2 State whether each of the following is true or false. If the answer is false, explain why.
The default
case is required in the switch
selection statement.
The break
statement is required in the default
case of a switch
selection statement to exit the switch
properly.
The expression (x > y && a <b)
is true
if either the expression x > y
is true
or the expression a < b
is true
.
An expression containing the ||
operator is true
if either or both of its operands are true
.
5.3 Write a C++ statement or a set of C++ statements to accomplish each of the following:
Sum the odd integers between 1 and 99 using a for
statement. Use the unsigned int
variables sum
and count
.
Print the value 333.546372
in a 15-character field with precisions of 1
, 2
and 3
. Print each number on the same line. Left-justify each number in its field. What three values print?
Calculate the value of 2.5
raised to the power 3
using function pow
. Print the result with a precision of 2
in a field width of 10
positions. What prints?
Print the integers from 1 to 20 using a while
loop and the unsigned int
counter variable x
. Print only 5 integers per line. [Hint: When x % 5
is 0, print a newline character; otherwise, print a tab character.]
Repeat Exercise 5.3(d) using a for
statement.
5.4 Find the errors in each of the following code segments and explain how to correct them.
unsigned int x{1};
while (x <= 10); {
++x;
}
for (double y{0.1}; y != 1.0; y += .1) {
cout << y << endl;
}
switch (n) {
case 1:
cout << "The number is 1" << endl;
case 2:
cout << "The number is 2" << endl;
break;
default:
cout << "The number is not 1 or 2" << endl;
}
The following code should print the values 1 to 10.
unsigned int n{1};
while (n < 10) {
cout << n++ << endl;
}
for
, while
.
after.
switch
.
continue
.
&&
(conditional AND).
false
.
False. The default
case is optional. Nevertheless, it’s considered good software engineering to always provide a default
case.
False. The break
statement is used to exit the switch
statement. The break
statement is not required when the default
case is the last case. Nor will the break
statement be required if having control proceed with the next case makes sense.
False. When using the &&
operator, both of the relational expressions must be true
for the entire expression to be true
.
True.
unsigned int sum{0};
for (unsigned int count{1}; count <= 99; count += 2) {
sum += count;
}
cout << fixed << left
<< setprecision(1) << setw(15) << 333.546372
<< setprecision(2) << setw(15) << 333.546372
<< setprecision(3) << setw(15) << 333.546372 << endl;
Output is:
333.5 333.55 333.546
cout << fixed << setprecision(2) << setw(10) << pow(2.5, 3) << endl;
Output is:
15.63
unsigned int x{1};
while (x <= 20) {
if (x % 5 == 0){
cout << x << endl;
}
else {
cout << x << ' ';
}
++x;
}
for (unsigned int x = 1; x <= 20; ++x) {
if (x % 5 == 0)
cout << x << endl;
}
else {
cout << x << ' ';
}
}
Error: The semicolon after the while
header causes an infinite loop.
Correction: Delete the semicolon after the while
header.
Error: Using a floating-point number to control a for
iteration statement.
Correction: Use an unsigned int
and perform the proper calculation to get the values.
for (unsigned int y = 1; y != 10; ++y) {
cout << (static_cast< double >(y) / 10) << endl;
}
Error: Missing break
statement in the first case
.
Correction: Add a break
statement at the end of the first case
. This is not an error if you want the statement of case 2:
to execute every time the case 1:
statement executes.
Error: Improper relational operator used in the loop-continuation condition.
Correction: Use <=
rather than <
, or change 10
to 11
.