C++ Programming Tutorial Lesson 07: For Loops

In the previous lesson on while loops, it was shown that a section of code can be run repeatedly while a condition is met. Once that condition is no longer true, the execution of the code jumps beyond the while loop to the next set of code. Sometimes, it is desirable to run a section of code a specific number of times. The for loop in C++ allows the programmer to accomplish such a task. Let's look at a for loop example that builds off of the Hello World example source code from Lesson 01: Hello World!.

For Loop Example #1

#include <stdlib.h>
#include <stdio.h>

int main(void) {
   int a;
   for (a = 0; a < 3; a++) {
      printf("Hello World!\n");
   }
   system("PAUSE");
   return 0;
}
Feel free to download the For Loop Example #1 source code directly. This program will print out "Hello World!" 3 times as shown below:



The table below describes the for loop C++ code presented in the above example.

Source Code Description & Explanation
int a;
This line of code defines an instance of an integer variable, "a". This variable will be used as a counter in the for loop.
for (a = 0; a < 3; a++) {
   printf("Hello World!\n");
}
This is the for loop. The for statement on the first line defines the initialization expression (a = 0), the conditional expression (a < 3), and the loop expression (a++). The initialization expression, a = 0, is a piece of code that is run only once upon entering the for loop. Here, we set variable "a" equal to zero, which effectively initializes our counter. The conditional expression, a < 3, is similar to that which is used in a while loop as shown previously in Lesson 06: While Loops. It is the condition under which the for loop will continue to run repeatedly, i.e. the for loop will run while this condition is met. Here, our condition is that variable "a" is less than 3. So, the for loop will run repeatedly while variable "a" is less than 3. The loop expression, a++, is a piece of code that is run after each loop completes in the for loop. Here, our loop expression is a++, which increments the value of variable "a" by 1. Therefore, after the printf() function is run to print out "Hello World!", variable "a" will be incremented by 1, and then variable "a" will be tested to see if it is less than 3. If it is less than 3, then the for loop will continue to run. Otherwise, the execution of the code will jump past the end curly brace of the for loop. So, variable "a" starts at 0 and it increments after each time "Hello World!" is printed onto the screen. When variable "a" reaches 3, the for loop ends. So, "Hello World!" will be printed out three times, when variable "a" is equal to 0, 1, and 2.

In order to illustrate the workings of the for loop, the following example shows an equivalent while loop definition that works exactly the same as the above for loop example.

While Loop Example #2

#include <stdlib.h>
#include <stdio.h>

int main(void) {
   int a;
   a = 0;
   while (a < 3) {
      printf("Hello World!\n");
      a++;
   }
   system("PAUSE");
   return 0;
}

Feel free to download the While Loop Example #2 source code directly. Why not just use a while loop instead of the for loop? Well, the for loop uses less lines of code compared to the while loop.

Copyright © 2008 Pierre Dufilie IV. All Rights Reserved.