Do Loops

The for loop is the most common type of loop; however, it is not the only type of loop. There are other loop structures you can use, although they all accomplish the basic goal of executing a given piece of code a specified number of times. The do loop is one such loop structure. Unlike the for loop, however, it does not evaluate whether or not the loop should continue until after the loop has completed an iteration. The following is the generic structure.

Do {    // place code here } while (some condition is true)

The following is a more specific example.

Example 5.5

Step 1:

#include <iostream> using namespace std; int main()    {   int x;  do  {    cout<< x  ; x++;   }while (x < 10);   return 0;    }

You can see that the do loop does essentially the same thing as the for loop. However, the loop condition is not evaluated until after the loop has executed an iteration, and the loop counter is incremented inside the loop. These loop structures give you different options for accomplishing the same basic goal.

C++ Programming Fundamentals
C++ Programming Fundamentals (Cyberrookies)
ISBN: 1584502371
EAN: 2147483647
Year: 2005
Pages: 197
Authors: Chuck Easttom © 2008-2017.
If you may any questions please contact us: