Compound Statement Within the for Loop

As with most statements in C++, you need to use the curly braces to indicate a compound statement in the body of a for loop:

int a;
for (a = 1; a <= 5; a++)
{
  cout << setw(5)  << a;
  cout << setw(10) << a * a;
  cout << setw(10) << a * a * a;
  cout << endl;
}


Output:

     1    1    1
     2    4    8
     3    9   27
     4   16   64
     5   25  125

Previous slide
Next slide

Back to Lesson 9 Index
Back to Outline