<< Chapter < Page | Chapter >> Page > |
There are two commonly used test before loops in the iteration (or repetition) category of control structures. They are: while and for. This module covers the: for.
In most programming languages the for loop is used exclusively for counting; that is to repeat a loop action as it either counts up or counts down. There is a starting value and a stopping value. The question that controls the loop is a test expression that compares the starting value to the stopping value. This expression is a Boolean expression and is usually using the relational operators of either less than (for counting up) or greater than (for counting down). The term loop comes from the circular looping motion that occurs when using flowcharting. The basic form of the for loop (counting up) is as follows:
for
initialization of the starting valuestarting value is less than the stopping value
some statements or actionsome statements or action
some statements or actionincrement the starting value
It might be best to understand the for loop by understanding a while loop acting like a counting loop. Let's consider;
initialization of the starting value
while the starting value is less than the stopping valuesome statements or action
some statements or actionsome statements or action
increment the starting value
Within the for control structure there are four attributes to a properly working loop. They are:
The initialization of the flag is not technically part of the while control structure, but it is usually part of the for control structure. The English phrasing is, "For x is 1; x less than 3; do the following actions; increment x; loop back to the test expression". This is doing the action on the true. When the test expression is false, you stop the loop and go on with the next item in the program. Notice, because this is a test before loop the action might not happen . It is called a test before loop because the test comes before the action. It is also sometimes called a pre-test loop, meaning the test is pre (or Latin for before) the action and update.
The syntax of the for loop control structure within the C++ programming language is:
for (initializations; expression; updates)
{statement;
statement;statement;
}
for (counter = 0; counter<5; counter++)
{cout<<"\nI love ice cream!";
}
The four attributes of a test before loop (remember the for loop is one example of a test before loop) are present.
Using indentation with the alignment of the loop actions is normal industry practice within the C++ community.
At this point it's worth mentioning that good programming always provides for a method to insure that the loop question will eventually be false so that the loop will stop executing and the program continues with the next line of code. However, if this does not happen then the program is in an infinite loop. Infinite loops are a bad thing. Consider the following code:
for (counter = 0; counter<5;)
{cout<<"\nI love ice cream!";
}
The programmer assigned a value to the flag during the initialization step which is correct. However, he forgot to update the flag (the update step is missing). Every time the test expression is asked it will always be true. Thus, an infinite loop because the programmer did not provide a way to exit the loop (he forgot to update the flag).
The following shows the use of the sequence operator to separate the multiple initializations and multiple updates. This is not available in most languages, thus is more unique to the C++ programming language.
for (x = 0, y = 10; x<10; x++, y--)
{cout<<x * y<<endl;
}
Below is a color coded the conversion of a while loop that displays a message exactly three times (which is a counting loop) into a for loop using C++ programming language syntax. The four loop attributes are color highlighted as follows:
Many languages (Pascal, FORTRAN, and other) have a for loop structure that is used exclusively for counting. The for loop in the C++ programming language is much more versatile and can be used (and generally is used) in place of the while loop structure. In reality a counting loop is just a particular use of a while loop.
The name for comes from mathematics’ method of writing an iteration (or repetition). In math we would say: “For the variable i starts at a given value and repeats an action increasing the value of i until i is executed for the stopping value”. Usually written in math as:
for i = 1 to 5 do some action
Note: here the = means equals not assignment. Another way to say it is that i varies from 1 to 5.
Notification Switch
Would you like to follow the 'Programming fundamentals - a modular structured approach using c++' conversation and receive update notifications?