For Loop

 [Previous Chapter]  [Previous Page]  [Contents]  [Next Page]  [Next Chapter]

Squares.C
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
   int limit;
   cout << "Limit: "; cin >> limit;
   for (int n(1); n <= limit; n += 1) {
      cout << setw(4) << n << setw(11) << n*n << endl;
   }
}

*for ( initialization ; condition ; expression ) statement

is almost equivalent to

{
   initialization ;
   while ( condition ) {
      statement
      expression ;
   }
}

 

*The initialization, the condition, and the expression may be empty. If the condition is omitted, the loop runs forever.
 
*The scope of n is limited to the for statement.
 

 [Previous Chapter]  [Previous Page]  [Contents]  [Next Page]  [Next Chapter]
Copyright © 2001, 2002 Andreas Borchert, converted to HTML on February 21, 2002