Exercises with Iteration
1) In the following C
code fragment, a semicolon appears at the end of the while statement.
What will be the result of executing this code?
printf ("A");
loopCount = 1;
while (loopCount <=3);
{
printf ("B");
loopCount++;
}
printf ("C");
2)
int loopCount;
while (loopCount <= 8)
{
printf("Hi");
loopCount ++;
}
What is missing from the C loop above?
- The initialization of the loop
control variable
- The testing of the loop control
variable
- The incrementing of the loop control
variable
- Nothing is missing.
3)
Indicate where (if at all) the following loop needs a "priming" read.
1 Sum = 0;
2 while (!Number > 0) {
3 Sum = Sum + Number;
4 scanf ("%d", Number);
5 }
4) Which of the
following would be a poor choice for a sentinel value (there may be
more than one)?
- A value of 999 for voterAge
- A value of 1 for testScore
- A value of 13 for birthdayMonth
- A value of 75 for carSpeed
5) Which type of loop
would be most appropriate for solving the problem "Print every other
input character until the character '@' is encountered"?
- Count-controlled
- Flag-controlled
- Sentinel-controlled
- EOF-controlled
- Either b or c above
6) Which type of loop
would be most appropriate for solving the problem "Input an integer
value, then print "Happy Birthday" that many times"?
- Count-controlled
- Flag-controlled
- Sentinel-controlled
- EOF-controlled
- Either b or c above
7) Which type of loop
would be most appropriate for solving the problem "calculate the sum of
all the odd integers in a data file of unknown length"?
- Count-controlled
- Flag-controlled
- Sentinel-controlled
- EOF-controlled
- Either b or c above