Loading

The For Loop

12/1/2015
When you need to perform repeated actions, over and over again, making a programming loop is often the preferred approach. This article will consider the traditional "for" loop, the one derived from the C programming language.

Interested in learning the C++ programming language? Try doing it with One Hour a Day.

When you need to perform repeated actions, over and over again, making a programming loop is often the preferred approach. The while and for loops are the most common types of loops. This article will consider the traditional "for" loop, the one derived from the C programming language. Other kinds of for loops are also available, depending on the programming language, and allow you to more conveniently loop over data types such as arrays and objects.

The syntax for the for loop is as follows:

for (initializer; condition; increment/decrement)
{
    // do these statements as long as condition holds true
}

The construct above is often the same for many programming languages. Some programming languages allow you to omit the parenthesis, others need you to place a colon (:) at the end of the first line, and so on. This article will consider the for loop using the C/C++ syntax.

The way the for loop works is it first initializes a certain variable (or variables). Then, it checks the condition to see if it is true. If the condition is true, the statements within the braces are executed. After that, the loop goes on to increment or decrement the loop control variable (which is often the one you had just initialized in the initializer list). Then, it checks for the condition to see if it still holds true. If so, then it goes on to run the statements again. Then it increments or decrements the loop control variable. Then it checks the condition again. This pattern repeats until the condition becomes false, whereupon the loop terminates and the program continues to do whatever it needs to do next.

Let us take a look a concrete example:

for (int i = 1; i <= 10; i++)
{
    cout << i << endl;
}

The above outputs the following:

1
2
3
4
5
6
7
8
9
10

Going the for loop above, first an integer variable named "i" is declared and then initialized with value 1. Then, the condition part is checked. Is 1 <= 10? Yes, it is true, so it executes the statements within the braces. In this case, it only outputs the value of the variable "i." After executing the cout statement, the variable "i" gets incremented: i++, going from value 1 to value 2. After that increment operation, the loop condition is once again checked. If the condition holds true, which it is (2 <= 10), then it goes on to execute the loop statements again. Then i gets incremented, and becomes 3. The condition is again checked (3 <= 10), holds true because the comparison yields a true value. The statements are once again executed, the loop control variable "i" is incremented, the condition is checked. This pattern repeats over and over until the loop condition becomes false, whereupon the loop terminates. In this case, that will be when i becomes 11. So in the end, the loop produces an output that counts from 1 to 10.

For loops are often used to go through the elements of an array:

// An array of integers with 8 elements
int myArray[8] = { 2, 3, 6, 3, 21, 14, 17, 1 };

// To hold the size of the array (i.e. number of elements)
int arraySize = 8;

for (int i = 0; i < arraySize; i++)
{
    cout << myArray[i] << endl;
}

The above outputs all the elements of the array, each in a new line. Note that for arrays, you start counting from 0 and the last element is always the size (or length) of the array minus one. Thus the for loop construct usually follows the format above.

Output:

2
3
6
3
21
14
17
1

In general, the for loop is used when you know specifically how many times to iterate. In the above example, the length of the array was known and it was expected the loop would repeat 8 times. When you do not really know how many times you are going to iterate or you would like some more flexibility and freedom in your loop, you would use a while loop.

Interested in learning the C++ programming language? Try doing it with One Hour a Day.

Did you like the lesson? 😆👍
Consider a donation to support our work: