The for Loop in C++

A for loop has a concise syntax for writing loops.

Often you write a loop in the following common form:

i = initialValue; // Initialize loop-control variable

while (i < endValue)

{

// Loop body

i++; // Adjust loop-control variable

}

A for loop can be used to simplify the above loop:

for (i = initialValue; i < endValue; i++)

{

// Loop body

}

In general, the syntax of a for loop is as shown below:

for (initial-action; loop-continuation-condition;

action-after-each-iteration)

{

// Loop body;

Statement(s);

}

The flowchart of the for loop is shown in Figure 5.3a.

Figure 5.3 A for loop performs an initial action once, then repeatedly executes the statements in the loop body, and performs an action after an iteration when the loop-c ontinuation-condition evaluates to true.

The for-loop statement starts with the keyword for, followed by a pair of parentheses enclosing initial-action, loop-continuation-condition, and action-after­each-iteration, followed by the loop body enclosed inside braces. initial-action, loop-continuation-condition, and action-after-each-iteration are separated by semicolons.

A for loop generally uses a variable to control how many times the loop body is executed and when the loop terminates. This is called a control variable. The initial-action often initializes a control variable, the action-after-each-iteration usually increments or decrements the control variable, and the loop-continuation-condition tests whether the control variable has reached a termination value. For example, the following for loop displays Welcome to C++! 100 times:

int i;

for (i = 0; i < 100; i++)

{

cout << “Welcome to C++!\n”;

}

The flowchart of the statement is shown in Figure 5.3b. The for loop initializes i to 0, then repeatedly executes the output statement and evaluates i++ while i is less than 100.

The initial-action, i = 0, initializes the control variable, i. The loop- continuation-condition, i < 100, is a Boolean expression. The expression is evaluated right after the initialization and at the beginning of each iteration. If this condition is true, the loop body is executed. If it is false, the loop terminates and the program control turns to the line following the loop.

The action-after-each-iteration, i++, is a statement that adjusts the control vari­able. This statement is executed after each iteration. It increments the control variable. Even­tually, the value of the control variable should force the loop-continuation-condition to become false. Otherwise, the loop is infinite.

The loop control variable can be declared and initialized in the fo r loop. Here is an example:

for (int i = 0; i < 100; i++)

{

cout << “Welcome to C++!\n”;

}

If there is only one statement in the loop body, as in this example, the braces can be omitted as shown below:

for (int i = 0; i < 100; i++)

cout << “Welcome to C++!\n”;

for (int i = 0, j = 0; i + j < 10; i++, j++)

{

// Do something

}

The action-after-each-iteration in a for loop can be a list of zero or more comma-separated statements. For example:

for (int i = 1; i < 100; cout << i << endl, i++);

This example is correct, but it is a bad example, because it makes the code difficult to read. Normally, you declare and initialize a control variable as an initial action and incre­ment or decrement the control variable as an action after each iteration.

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *