Loops are extremely useful. In fact, if there wasnt loops, we would
still be using the famous GOTO technique. There are several ways to make
loops in c and c++. These loops repeat a certain block of code over and over
again until a certain condition is met. Sometimes, as in game loops on
consoles, those conditions are never met, hence it isnt required to meet
those conditions. Anyway, lets take a look at a nice little loop;
/* Getting old
Written by VerticalE */
int myAge = 8;
for(int i = 0 ; i<10 ; i = i + 1)
{
prinf("My age is %d\n", myAge");
myAge = myAge + 1;
} |
Woah! So much new code! If you run off screaming, dont worry. This
document wont go anywhere (but it will strangle you in your sleep). First of
all, this code here printf "My age is X" on the screen, repeatedly. What it
does repeat, is the things that belong to for(); Yes, just as with the
main(), the for-loop has to curly brackets that enclose "some code". We call
this code (just the code that is enclosed) for a block. Everything that is
enclosed in these curly brackets is a code-block. Now lets take a closer
look at the block before we look at the actual loop.
printf(); you are already familiar with. However, there is a difference this
time. As I said earlier, it printf "My age is X" on the screen. X, in this
case is the variable myAge. When the printf is executed, it prints whatever
value myAge have. At the first time of the loop, myAge is 8, and so it
prints "My age is 8". This is very easy to understand, because when I say X,
you know that X can hold any value ("I have X monkeys" could mean just about
any amount of monkeys, but it could also mean that "I have ass monkeys",
whatever they are...). When the computer reads printf"My age is -> and comes
to the %d, it thinks to itself "Aha! This is an X! This is a variable!". It
knows that it isnt supposed to print "%d" on screen. Seeing as this is the
first variable (or X) that it has stumbled upon, it looks over the
sentence's shoulder to find the myAge variable waiting. It then checks to
see the value of myAge, and then prints it along with the sentence.
Easy, eh? "But what about the \n, and why does it say %d? Can't it just say
X instead?" Well, the compiler needs to know what type of variable it is, so
that it can print it on screen. You see, it prints different variables
differently. %d means that it is to print an integer value. So basically, it
means that X in this case, just isnt X. It's like telling your friend "Hey,
guess what I got from robbing little-ol ladies yesterday? I wont say what it
was, except that it is a number value between -32,000 to 32,000". Ok, that
lame, but you get the idea. Ok, lets disregard the /n for a moment here. If
you where to run
this loop several times, what would happen? It would print "My age is 8My
age is 9My age is 10My age..." and so on. It would hardly look pretty on
screen. \n is what we call an "escape character". It does a specific action
when executed, and in this case it does two things; "carriage return" and
"line feed". Think of the cursor (the write cursor, not the mouse) as the
carriage. Carriage return means that it jumps back to the start of the line
again, just like pushing "home". Line feed simply means that it moves the
carriage one line down. The whole process is, to put it simply, the same
that happens when you press "Enter" in word (which is why the key was called
Return on old computers). With the \n present in out code, the output would
look like this;
My age is 8
My age is 9
My age is 10
...
Simple! If you understand everything up to this point, give yourself a pat
on the back. If not, read it over again, because you need to know this if
you want to move on. Anyways, lets take a look at the next part of our
block.
myAge = myAge + 1;
This is a statement, so it ends with a semicolon. It is also, as they say, a
mathematical statement. Calculations in C/C++ are always done on the right
side, and the answer is then transferred to the left. Yep, just opposite of
what your used to from school. myAge + 1 means practically "the value of the
variable myAge + 1", but here comes the tricky part. The result of the
calculation is then stored in the myAge variable. This means, that whatever
the value is before this statement is executed, the variable myAge get + 1
added to itself. This operation, adding 1 to a value, is so common in
programming that is has got a simple little function that do just that. The
two statements below produce the same result:
myAge = myAge + 1;
myAge++;
The last statement is called "incrementation". Incrementation = add a value
to an excisting value. You also have the opposite, decrementation, which
(duh) decrements the value by one. You just use the double minus sign
instead of the plus sign.
myAge--;
We want the age to increase (well, not if your on old geezer), so we'll
stick with incrementation. Let's update our code:
/* Getting old
Written by VerticalE */
int myAge = 8;
for(int i = 0 ; i<10 ; i = i + 1)
{
prinf("My age is %d\n", myAge");
myAge++;
} |
That's all for this part of the article. In the next part, we'll take a
look at all the scary stuff outside of the codeblock. |