Sams Teach Yourself C++ in 21 Days

(singke) #1
better—and more in line with the current standards—to use an expression that
evaluates to a Boolean value of true or false. However, a variable of type boolcan
be used in a condition without any potential problems.

Workshop ............................................................................................................


The Workshop provides quiz questions to help you solidify your understanding of the
material covered as well as exercises to provide you with experience in using what
you’ve learned. Try to answer the quiz and exercise questions before checking the
answers in Appendix D, and be certain you understand the answers before continuing to
tomorrow’s lesson.

Quiz ................................................................................................................



  1. How do you initialize more than one variable in a forloop?

  2. Why is gotoavoided?

  3. Is it possible to write a forloop with a body that is never executed?

  4. What is the value of xwhen the forloop completes?
    for (int x = 0; x < 100; x++)

  5. Is it possible to nest whileloops within forloops?

  6. Is it possible to create a loop that never ends? Give an example.

  7. What happens if you create a loop that never ends?


Exercises ........................................................................................................



  1. Write a nested forloop that prints a 10×10 pattern of 0s.

  2. Write a forstatement to count from 100 to 200 by twos.

  3. Write a whileloop to count from 100 to 200 by twos.

  4. Write a do...whileloop to count from 100 to 200 by twos.
    5.BUG BUSTERS:What is wrong with this code?
    int counter = 0;
    while (counter < 10)
    {
    cout << “counter: “ << counter;
    }
    6.BUG BUSTERS:What is wrong with this code?
    for (int counter = 0; counter < 10; counter++);
    cout << counter << “ “;


206 Day 7

Free download pdf