and perhaps announce it somewhere else. The title is just a humor on a topic
for this post: the "for" loop.
The for loop is a special case that requires extensive coverage. It behaves
somewhat like a while loop but it has numerous twists. First, we have to set
three things or less: the starting value, the termination value and an
incrementor. The starting value, or index is where the for loop begins. The
termination index is the value that tells the program that we are done with
the loop. The incrementor is meant to tell the program the course of this
loop e.g. move step-by-step or increment by 1. I know this is somewhat hard
to explain - I was in that position when I first learned for loop.
The English version of the syntax is:
for (StartIndex; TerminationIndex; Incrementor)
// Do whatever...
The more generalized C++ example is:
for (int i = 0; i < whatever; i++)
// The content of the loop...
For instance, suppose if we want to have the program print out numbers
between 1 and 10 in sequence i.e. one by one, we can do this: first, set the
initial value to 0. Next, set the ending value to 10 and have the loop
operate until the value reaches ten. Then increment this loop step-by-step
i.e. one by one. The content of the loop (between braces) would be the
output code. Let's see how this can be translated into C++:
#include <iostream> #include <string> // The usual code to include iostream
using namespace std;
int main(); // The start of our program (int main();).
// The for loop to print numbers from 1 to 10:
for (int i = 0; i < 10; i++)
cout << i+1 <, " ";
cout << endl;
If we compile this program and run it, we get:
1 2 3 4 5 6 7 8 9 10
There is a reason why I didn't declare any int variables on the main block:
that variable is declared as part of the for loop declaration, and we want
to just add 1 inside the loop to print the correct number. Alternatively, I
could have done this task using more familiar way, as in the following
description and code:
// An alternative way...
for (int i = 1; i <= 10; i++)
cout << i << " ";
cout << endl;
Notice that we have started counting at 1, which is in keeping with more
common sense terms. Also, you might recognize that we end our loop when the
count reaches 10 (as evidenced by less-than or equal to operator). In other
words, we can start at wherever we want and terminate it whenever we are
finished with it. But for general practice, I personally suggest that you
use zero (0) as the starting point.
As an additional trick, we can ask our computers to check whether the first
entry of the for loop is right or not. If the entry is not what the computer
is looking for, we can ask the user to type it again. if the entry is
correct, then we can continue running for loop. This uses all conditional
statements we've looked at so far - if, while and for. One of the review
exercises later will ask you to try out this problem.
As we have seen, the for loop is used to repeatedly execute a program a
number of times before continuing with our next step. I'll come back to for
loop later when we discuss arrays and vectors - how to store additional data
and print what we have in there.
P.S. There is a reason why I brought up counting problem. I'll discuss this
as part of our detour into hardware stuff after the review exercises.