|
|
|
|
Back to Bluesfear |
|
C++
- For Loops
Looping
is probably one of the most important programming concepts in existence. There
are so many applications of loops it would be impossible to list them all.
To name a few though, things like parsing a string, trapping for errors, and
animation. Also, since it will allow you to execute a block of code over and
over, it saves time and typing. Now, depending on what kind of programming
language you came from, loops might already be familiar to you, but if you've
come from a low level programming language such as assembly or (god forbit)
GWBasic, you are probably more familiar with jump or goto statements. Well,
loops take into account all that lovely comparison crap you used to have to
do before and puts it into one nice package. The easiest and most used of
these loops is the for loop.
The syntax
of the for loop is as follows...
for
(var; condition; increment)
for
(var; condition; increment) {
...
}
Note
the semi-colons between the different parts of the loop.
If
you are familiar with loops then you know that a counter variable is needed
(obviously) to control your loop. Something to tell your loop how the hell
to get out. There are ways to get out of a loop with no condition, but why
make things more complicated than we really need to right? So, var is the
name of our counter variable. Usually we would use an integer here, or perhaps
a double type variable. You could probably get away with a char type, but
I can't see much application in a string as a counter... not in a for loop
anyways. Condition is the test to see if the loop should continue. If condition
evaluates to true, the loop will execute once, then the increment (how much
we modify the variable by) is incremented, and the condition tested once more.
The condition is any boolean logic test as described in chapter I. Just so
you can see a loop in action, here is a quick code excerpt to demonstrate.
int
i = 0;
for
(i; i < 10; i++)
cout << i << endl;
Notice
a few things. We start the loop by using i as our counter variable. Then we
place a condition on loop execution. The loop will execute so long as the
value of i remains less than 10. Lastly, our increment is i++ which means
we want to add one to i each cycle through the loop. Now, a couple time savers
in this process is that we can initialize our variable when we tell the loop
which variable we are using as our counter variable. Also, a new addition
for C++ over C is that we can define a variable on the fly to use as a counter.
Something to be aware of though is that the scope of this variable is the
loop itself. Although it appears that in some compilers the variable continues
to exist after the loop even though has finished. Confusing yes? I thought
so. Just assume that your variable goes out of scope and dies when your loop
finishes if you've declared your counter within the loop. Another thing to
note is that loops can be nested (one loop inside the other) just like if
statements. Here are some more examples of loops.
#include
<iostream.h>
int
main(void) {
cout << "First Loop" <<
endl << "----------" << endl;
int x = 3;
cout << "Initial value of x (before
loop): " << x << endl << endl;
for (x =
1; x < 42; x = x * 2)
cout << "Inside loop, value
of x: " << x << endl;
cout << endl << "Final value
of x (after loop): " << x << endl << endl;
cout << "Second Loop" <<
endl << "-----------" << endl;
for (int i = 0; i < 3; i++)
for
(int j = 0; j < 3; j += 1)
for
(int k = 0; k < 3; k = k + 1)
cout << "[ "
<< i << " " << j << " " <<
k << " ]" << endl;
return 0;
}
output
------
First
Loop
----------
Initial
value of x (before loop): 3
Inside
loop, value of x: 1
Inside
loop, value of x: 2
Inside
loop, value of x: 4
Inside
loop, value of x: 8
Inside
loop, value of x: 16
Inside
loop, value of x: 32
Final
value of x (after loop): 64
Second
Loop
-----------
[
0 0 0 ]
[
0 0 1 ]
[
0 0 2 ]
[
0 1 0 ]
[
0 1 1 ]
[
0 1 2 ]
[
0 2 0 ]
[
0 2 1 ]
[
0 2 2 ]
[
1 0 0 ]
[
1 0 1 ]
[
1 0 2 ]
[
1 1 0 ]
[
1 1 1 ]
[
1 1 2 ]
[
1 2 0 ]
[
1 2 1 ]
[
1 2 2 ]
[
2 0 0 ]
[
2 0 1 ]
[
2 0 2 ]
[
2 1 0 ]
[
2 1 1 ]
[
2 1 2 ]
[
2 2 0 ]
[
2 2 1 ]
[
2 2 2 ]
Analyse
this code and the output it generates and try to understand what happens.
One thing to note is that the condition test of the for loop is always exeucted
after the increment has been evaluated. This is why x takes the value of 64
at the end of the loop instead of keeping the last value it had inside the
loop. Also, take note of the increment statement in the nested for loops.
Each of these does exactly the same thing, increment the counter by one. It
demonstrates the different arithmetic methods that can be used here.
Lastly,
I just want to bring to your attention that it is possible that a for loop
will not execute even once. The condition is tested before the loop progresses
so if your condition evaluates as false the first time through the loop, the
block of code for the loop will not execute.
Written For: Omicron of http://www.bluesfear.com
Back to Bluesfear
Digital Art
2000, 2004© BlueSfear