the best use of a for loop. ever. |
Loops. I like them. They make things much easier by allowing me to do less work. Loop work by doing an action multiple times without you having to write it again and again. The above cartoon from Bill Amend's FoxTrot does a great job of explaining how and why a FOR LOOP can be used. Here’s the code from the strip:
int main(void)
{
int
count;
for(count
= 1; count <= 500; count++)
printf(“I
will not throw paper airplanes in class.”);
return
0;
}
You
should be able to recognize the first line from our talk on functions. int means this will be what’s returned by the function, main is the name of the function, (void) is
basically saying that main cannot have any parameters, which
we might not have covered. I don’t remember.
The
next line declares an integer variable of count, which will be used in the for
loop.
The
next line is the FOR LOOP and it might be a bit confusing. Let’s break it down
into simpler thought first. Here’s the bits:
for() is the function name
count = 1 sets our variable to 1
count <= 500 check to make sure that count is less than or equal to 500
count++ is adding one (1) to our count variable
printf() is the action we preform after we do this count check
Make
note that the prinf() is not enclosed in brackets. Even
though functions have brackets, this is allowable because there is only one
line of code inside that function. If we were to put anything else in there,
say another printf() then we would have to use the
brackets. I tend to use brackets regardless of the line count, simply because I
never know when I’ll need to add more lines into a function. I believe general
philosophy of programmers is to not do this as it makes the code pretty long
and ugly, but they’re wrong. :P
Moving
on. Let’s get into the nitty-gritty of what a for loop is really doing. We
first set a variable count to 1. We then check to see if count is less than/equal to 500. If it is then we add 1 to count (++
means add one, -- means subtract one). Then we would print out “I will not throw paper airplanes in class.” Then the loop starts again. It will
continue to print out this phrase until it has reached 500. On the 501st
loop it will stop printing and then move on to the next step in the function:
return 0 just returns a false value to our main function
And
that’s a for loop. If you understood what I was explaining, then you’ll now
understand the FoxTrot comic. Jason, the boy in the strip, was told to write “I will not throw paper airplanes in class.” 500 times. Instead of writing that
line 500 times he automated it so the “computer” would simply print out the
line 500 times.
With
just two lines of code you can save a whole bunch of copy/paste and more
importantly your time so instead of this:
printf(“I will not throw paper airplanes in
class.”);
printf(“I will not throw paper airplanes in
class.”);
printf(“I will not throw paper airplanes in
class.”);
printf(“I will not throw paper airplanes in
class.”);
printf(“I will not throw paper airplanes in
class.”);
printf(“I will not throw paper airplanes in
class.”);
printf(“I will not throw paper airplanes in
class.”);
printf(“I will not throw paper airplanes in
class.”);
printf(“I will not throw paper airplanes in
class.”);
printf(“I will not throw paper airplanes in
class.”);
x 50
You
simply write this:
for(count = 1; count <= 500; count++)
printf(“I
will not throw paper airplanes in class.”);
Hopefully
you understand what a for loop is and what you can do with it. But before we go
let’s take a look at a different type of loop, called the WHILE LOOP. Instead
of using numbers like we did in the for loop this will check a condition.
Here’s a small while loop:
while(!gamePaused)
{
hero.controls();
hero.move();
monster.move();
game.controls();
}
haveParty();
Now
this isn’t real code as we haven’t actually defined our functions but here’s
the breakdown. The first line is looking to make sure that the game is not
paused. If it is running then it will allow controller input for our hero, both
the hero and monsters to move, as well as checking to see if the controller is
being used for game systems, like pausing the game for instance. Then if the
game is still not paused it will run through everything that’s in the while loop again. It will continue to do this until something changes
the variable gamePaused from false to
true. Then the function will be
completed and the next line of code will run, meaning the computer will have a
party! Yay!
This
might not be as cut and dry as the for loop, but while loops pretty much run
everything, whether it’s behind the code or a part of it. Games are built
around constantly updating and a while loop does just that. So long as the
condition is met, it will continue to run.
Well,
that’s it for this one. I’m hoping to have something else to write about early
next week, but I have to decide what else to cover in a basics course. Until
next time, God bless and code everyday!
Part 1: Variables | Part 2: Functions | Part 3: Operators
Part 1: Variables | Part 2: Functions | Part 3: Operators
No comments:
Post a Comment