Programming Tutorials

for Loops in C++

By: Baski in C++ Tutorials on 2007-09-09  

When programming while loops, you'll often find yourself setting up a starting condition, testing to see if the condition is true, and incrementing or otherwise changing a variable each time through the loop. The following program demonstrates this.

While reexamined.

1:    // 
2:    // Looping with while
4:    #include <iostream.h>
6:    int main()
7:    {
8:      int counter = 0;
10:      while(counter < 5)
11:      {
12:           counter++;
13:           cout << "Looping!  ";
14:      }
16:      cout << "\nCounter: " << counter << ".\n";
17:       return 0;
18: }
Output: Looping!  Looping!  Looping!  Looping!  Looping!
Counter: 5.

Analysis: The condition is set on line 8: counter is initialized to 0. On line 10, counter is tested to see whether it is less than 5. counter is incremented on line 12. On line 16, a simple message is printed, but you can imagine that more important work could be done for each increment of the counter.
A for loop combines three steps into one statement. The three steps are initialization, test, and increment. A for statement consists of the keyword for followed by a pair of parentheses. Within the parentheses are three statements separated by semicolons.

The first statement is the initialization. Any legal C++ statement can be put here, but typically this is used to create and initialize a counting variable. Statement 2 is the test, and any legal C++ expression can be used here. This serves the same role as the condition in the while loop. Statement 3 is the action. Typically a value is incremented or decremented, though any legal C++ statement can be put here. Note that statements 1 and 3 can be any legal C++ statement, but statement 2 must be an expression--a C++ statement that returns a value. The program below demonstrates a for loop.

Demonstrating the for loop.

1:      //
2:      // Looping with for
4:      #include <iostream.h>
6:      int main()
7:      {
8:        int counter;
9:        for (counter = 0; counter < 5; counter++)
10:          cout << "Looping! ";
12:        cout << "\nCounter: " << counter << ".\n";
13:         return 0;
14: }
Output: Looping!  Looping!  Looping!  Looping!  Looping!
Counter: 5.

Analysis: The for statement on line 8 combines the initialization of counter, the test that counter is less than 5, and the increment of counter all into one line. The body of the for statement is on line 9. Of course, a block could be used here as well.

The for Statement

The syntax for the for statement is as follows:

for (initialization; test; action )

The initialization statement is used to initialize the state of a counter, or to otherwise prepare for the loop. test is any C++ expression and is evaluated each time through the loop. If test is TRUE, the action in the header is executed (typically the counter is incremented) and then the body of the for loop is executed. Example 1

// print Hello ten times
for (int i = 0; i<10; i++)
cout << "Hello! ";

Example 2

for (int i = 0; i < 10; i++)
    cout << "Hello!" << endl;
    cout << "the value of i is: " << i << endl;

Add Comment

* Required information


No comments yet. Be the first!

Most Viewed Articles (in C++ )

Latest Articles (in C++)