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

Review Questions

  1. What would be output by the following segment of C++?
    int i;
    for ( i = 1; i <= 12; i *= 2 )
      cout << i << endl;
    

  2. Do questions 3 and 4 of Lesson 16 again using a for statement.
  3. What is printed by the following segment of C++?
    int i;
    for (i=1; i<20; i = i+3)
        cout << i << endl;
    
    What would happen if the i+3 in the update expression was changed to i-3?
  4. Write a for statement to output the numbers 1 to 20, each on a new line.
  5. Write a segment of C++ using a for statement which accepts n real values entered by a user and outputs their average value. Assume n will be greater than zero.
  6. Write a segment of C++ which enters a value for $n$, $n \geq 0$ and outputs the value of $2^n$. Use a while statement first, then repeat using a do-while statement and finally repeat using a for statement.



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