This document discusses a specific coding technique used in the C++ Language to code leading descision loops. Readers of this document may benefit from a review of Flowcharting Symbols, Logical Control Structures, and the web pages entitled Example of a Counting Loop (Repetition Structure) and Analysis of an accumulation using the repetition structure.
When coding a counting controlled loop, the programmer must deal with at least four parts:
These elements do not always occur in the order shown above, but they are always present (in counting controlled loops).
Typically, each one is coded by typing one or more individual statements. However in certain circumstances
a special statement can be used that organizes the four parts into one "automatic loop"
statement known as the for statement. Consider the counting loop shown in the following flowchart
(or [outlined algorithm]):

We could write C++ Language source code to perform those steps using the while statement.
#include <iostream> /* Standard Input/Output header file */
using namespace std;
int C; /* Counter */
int main ()
{
C = 1; /* Initialize counter to start at one */
while (C<=5) /* Test for repetition/exit BEFORE the body */
{ /* Start a pass through the loop */
cout << "Hello\n"; /* This step is the "body" of the loop */
C = C + 1; /* Increment C by one */
} /* End the pass through the loop */
return 0; /* Return zero error code to parent process */
}
The four parts of the counting loop are shown above in boldface.
We need braces { } surrounding the body and increment here. The while statement performs
the test and if the parenthesized condition is true, executes the single (or braced) statement(s) beyond
the while statement. Then control branches back to the while statement to perform
the test again. If the condition is false, control branches ahead (in the code) to the next statement
following the single (or braced) statement(s) beyond the while statement (in this example: return (0)).
The same steps code be coded using the for statement like this:
#include <iostream> /* Standard Input/Output header file */
using namespace std;
int C; /* Counter */
int main ()
{
for (C=1; C<=5; C=C+1) cout << "Hello\n";
return (0);
}
Notice that the automatic loop statement organizes the four parts of the counting loop
into one statement in the following order: (1) Initialize, (2) Test Condition, (3) Increment,
and finally the step (or braced steps) in the body. In this example, we no longer need
braces { } surrounding the body. After the first three parts of the loop are specified in the
parenthesized code following the keyword for, all we are left with is one statement.
So the braces are not necessary. When you have more than one statement remaining to repeat,
then you have to enclose them in a pair of braces.
The for statement performs in the same manner as the while statement,
except that the order of the four parts of the loop are fixed. The parts must be performed in the
order shown in the flowchart. If any of the steps are reorganized, then the process would
have to be coded using a separate while statement combined in the proper order with
statements to accomplish the other steps in the loop.
So remember, to use the for statement, a loop must be ordered with a leading
decision and with the increment as the last step in each pass of the loop.