Next: Exercises Up: The while statement Previous: Multiple Choice Questions

Review Questions

  1. What would be output by the following segment of C++?
    int fact, i;
    fact = 1;
    i = 2;
    while (i <= 6)
       fact *= i;
       i++;
    cout << fact;
    
    If the purpose of this segment of C++ was to output the value of 1*2*3*4*5*6 how should it be changed?
  2. Consider the following piece of C++
    int i;
    while (i < 10)
       {
         cout << i << endl;
         i++;
       }
    
    To what should i be initialised so that the loop would be traversed 10 times? In this case what would be printed out? How would you change the body of the loop so that with the same initialisation the numbers 1 to 10 would be printed? If the body of the loop was kept as it is above how should the initialisation and the condition be changed so that the numbers 1 to 10 are printed out?
  3. Write C++ statements using a while statement to print n asterisks at the beginning of a new line.
  4. Write C++ statements using a while statement to evaluate n!, i.e. 1*2*3*...*n, 0! is 1.



Next: Exercises Up: The while statement Previous: Multiple Choice Questions