C++ Programming Fundamentals (Cyberrookies)
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.
Категории