In this chapter we are going to do a simple for loop. It's really quite simple, and it's not as hard as some might think. Well kiddies, let's begin;

#include < stdio.h >
main ( )
{
int i;
for (i=0; i<100; i++){
printf ("Oh digitalscribe your so fine, your so\n");
printf ("fine you blow my mind hey diggy, hey diggy\n");
}
return (0);
}

Well here we have some stuff that on the surface may seem a little deep but really it's not. Almost everything in the preceding block of code should be obvious, except for line 5 and 8 and whats in between. Let's look at line 5 shall we?

for (i=0; i<100; i++){

The for keyword is the what sets up the loop. Everything between the parenthesis (the argument) is what sets up the parameters of the loop. Above this line of code you see that an int variable was set up. In the for loop we have (and I realize I'm being redundant) set the parameters of that variable. Let's look deeper and explain the argument.

i=0;

Here we have set the i variable to 0. This is the initialazation of what will be a "sort of" counter.

i<100;

Here we have set the condition of the loop. This says that the loop will cycle as long as i is "less than" 100. As soon as i is equal to 100 than the argument is not less than 100 and it will stop. I guess you realize that I'm using the word argument a lot. It's basically programming talk for parameter.

i++

This increments(makes it move up one digit). It tells i to basically continually move up

Anyways to put in pseudo code;

for (start count at 0; set it go 99 times; make i go){

Now that we have explained all that lets see how that little snippit of code relates to whats between the for brackets ({ }). Let us employ our abstract thinking shall we? Now this is just an analogy of sorts, don't take it literally. NOT ALL ANALOGIES ARE PERFECT!!!!!

Imagine that i is a aerobics instuctor. It goes into a gym and sees the front desk. The insturctor has no idea what his class is or what he's kind of excersie routine that he is supposed to lead his students through. The office coordinator (for) holds what the excersise routine is supposed to be. He is supposed to lead his students into 99 straight repititons of calestenics. So he goes into the gym (in the case whats between the for brackets) and starting with zero he goes through the routine 99 times and stops. In our case the 2 printf statements will be done 99 times and will stop.

Whew!!! That was a mouthful!!

A note on the incrementing thing. If you wanna use it to decrement you would use i--. However don't use it on what we just wrote....that would be screwey hehehe. Anyways I'll write some more stuff on loops later, I'm getting tired. -Randimus

back