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?

  1. The initialization of the loop control variable
  2. The testing of the loop control variable
  3. The incrementing of the loop control variable
  4. 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)?

5) Which type of loop would be most appropriate for solving the problem "Print every other input character until the character '@' is encountered"?

6) Which type of loop would be most appropriate for solving the problem "Input an integer value, then print "Happy Birthday" that many times"?

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"?