Chapter 6
LOOPING
QUESTIONS
True/False
1. The logical order of statements in a program may be different from their physical order.
2. When a While expression evaluates to FALSE, the loop terminates and control goes back to the statement immediately before the While statement.
3. In the C++ program fragment
count = 1;
while (count < 10)
count++;
cout << "Hello";
the output statement that prints "Hello" is not part of the body of the loop.
4. An infinite loop is one in which the While expression always has the value FALSE.
5. It is possible for the body of a While statement never to be executed.
6. The termination condition for the While loop
while (loopCount < 9)
{
cout << loopCount << endl;
loopCount++;
}
is
loopCount > 9.7. If a While loop's termination condition becomes true in the middle of the loop body, the loop is exited immediately.
8. In C++, an infinite loop results from using the assignment operator in the following way:
while (gamma = 2)
{
...
}
9. A program is said to be robust if it can recover from erroneous input and keep running.
Multiple Choice
10. What is the value of
loopCount = 1;
while (loopCount <= 145)
{
alpha = alpha + 7;
loopCount++;
}
a. 1
b. 144
c. 145
d. 146
11. What is the value of
someInt after control exits the following loop?someInt = 273;
while (someInt > 500)
someInt = someInt - 3;
a. 270
b. 273
c. 497
d. 500
e. none of the above--this is an infinite loop
12. What is the termination condition for the following While loop?
while (beta > 0 && beta < 10)
{
cout << beta << endl;
cin >> beta;
}
a.
beta > 0 && beta < 10b.
beta >= 0 && beta <= 10c.
beta < 0 || beta > 10d.
beta <= 0 || beta >= 1013. What is the output of the following code fragment?
n = 1;
while (n <= 5)
{
cout << n << ' ';
n++;
}
a.
1 2 3 4 5b.
1 2 3 4c.
1 1 1 foreverd.
2 3 4 5e.
2 3 4 5 614. What is the output of the following code fragment?
n = 1;
while (n < 5)
{
cout << n << ' ';
n++;
}
a.
1 2 3 4 5b.
1 2 3 4c.
1 1 1 foreverd.
2 3 4 5
e.
2 3 4 5 615. What is the output of the following code fragment? (Be careful here.)
n = 1;
while (n <= 5)
cout << n << ' ';
n++;
a.
1 2 3 4 5b.
1 2 3 4c.
1 1 1 foreverd.
2 3 4 5e.
2 3 4 5 616. What is the output of the following code fragment?
n = 1;
while (n <= 5)
{
n++;
cout << n << ' ';
}
a.
1 2 3 4 5b.
1 2 3 4c.
1 1 1 foreverd.
2 3 4 5e.
2 3 4 5 617. Which of the following would be a poor choice for a sentinel value?
a. a value of 999 for
voterAgeb. a value of 1 for
testScorec. a value of 13 for
birthdayMonthd. a value of 75 for
carSpeede. b and d above
18. With respect to the loop in the following
int main()
{
int loopCount;
while (loopCount <= 8)
{
cout << "Hi";
loopCount++;
}
return 0;
}
a. the initialization of the loop control variable
b. the testing of the loop control variable
c. the incrementation of the loop control variable
d. Nothing is missing.
19. Indicate where (if at all) the following loop needs a priming read.
sum = 0; // Line 1
while (inFile) // Line 2
{ // Line 3
sum = sum + number; // Line 4
inFile >> number; // Line 5
} // Line 6
a. between lines 1 and 2
b. between lines 2 and 3
c. between lines 3 and 4
d. between lines 4 and 5
e. No priming read is necessary.
20. Indicate where (if at all) the following loop needs a priming read.
count = 1; // Line 1
while (count <= 10) // Line 2
{ // Line 3
cin >> number; // Line 4
cout << number * 2; // Line 5
count++; // Line 6
} // Line 7
a. between lines 1 and 2
b. between lines 3 and 4
c. between lines 5 and 6
d. between lines 6 and 7
e. No priming read is necessary.
21. Given the input data
25 10 6 -1
what is the output of the following code fragment? (All variables are of type
int.)sum = 0;
cin >> number;
while (number != -1)
{
cin >> number;
sum = sum + number;
}
cout << sum << endl;
a. 15
b. 41
c. 40
d. 16
e. no output--this is an infinite loop
22. After execution of the following code, what is the value of
length = 5;
count = 4;
while (count <= 6)
{
if (length >= 100)
length = length - 2;
else
length = count * length;
count++;
}
a. 600
b. 100
c. 98
d. 20
e. none of the above
23. In the following code fragment, a semicolon appears at the end of the line containing the While condition.
cout << 'A';
loopCount = 1;
while (loopCount <= 3);
{
cout << 'B';
loopCount++;
}
cout << 'C';
The result will be:
a. the output
ACb. the output
ABCc. the output
ABBBCd. a compile-time error
e. an infinite loop
24. What is the output of the following code fragment? (All variables are of type
sum = 0;
outerCount = 1;
while (outerCount <= 3)
{
innerCount = 1;
while (innerCount <= outerCount)
{
sum = sum + innerCount;
innerCount++;
}
outerCount++;
}
cout << sum << endl;
a. 1
b. 4
c. 10
d. 20
e. 35
Fill-In
25. A control structure that causes a sequence of statements to be executed repeatedly is called a(n) ____________________.
26. A(n) ____________________ is an individual pass through, or repetition of, the body of a loop.
27. Loop ____________________ is the moment that repetition of the loop body ends and control passes to the first statement following the loop.
28. The condition that causes a loop to be exited is called the ____________________.
29. A(n) ____________________-controlled loop is one in which the number of repetitions is known in advance.
30. A(n) ____________________-controlled loop is one that terminates when something happens inside the loop body to signal that the loop should be exited.
31. A(n) ____________________-controlled loop is an event-controlled loop whose event is the input of a special value.
32. A(n) ____________________-controlled loop is an event-controlled loop whose event is the attempt to read past the end of an input file.
33. A(n) ____________________ is an input statement located before the While loop that processes its input data.
34. A(n) ____________________ is a counter variable that is incremented with each iteration of a loop.
35. A(n) ____________________ is a counter variable that is incremented each time a particular event occurs.
36. Loop ____________________ is the moment that the flow of control reaches the first statement inside the loop body.
37. Before each iteration of a While loop, the loop ____________________ is performed at the beginning of the loop.