Thursday, November 13, 2008

For/Next Loops

For/Next Loops are like steps, or stairs, in loops. They allow things to be done much, much quicker, with a lot less lines of code than if you wrote the whole program without loops. How are they stairs, however? Well, these loops move from one loop to another, before redoing the original loop. Confusing? Well, let's get into some more detail!

In these type of loops, you do not use the words "do" or "loop". Instead, you use "for" and "next"

An example of a program using this code is as followed, with me explaining the code~

For insertvariable1= # to # Step #

This line uses a variable, and it allows you to change the number automatically in the loop, with whatever number you want. Essentially, it goes through all these numbers, with a common stepping stone (2, 4, 6... etc).

for insertvariable2= # to # Step #
This line is just like the above, except it's inside the loop. So, when you do the original loop for variable1, you go through this loop as well. Trust me, you'll understand what I'm talking about later.

inservariable2=insertvariable1*insertvariable2
This code will multiple whatever number the first loop is at, with all the different numbers the second loop contains. Essentially, the first number that you are asking to original loop to use will multiply all the numbers that you are 'stepping' through in the second loop.

next insertvariable2
This tells you to redo the loop you are currently in, with the next number in the step.

next insertvariable1
This redoes the whole original loop, doing everything it contains all over again, but with the next number.

*NOTE* This code does not display the information in the form. Common etc.print codes can do the trick, and will be put into each specific loop in order to write the code for that loop, redoing everything.

As you can see, it's some pretty advanced stuff, but I'm sure I'll learn it all later on, and become much more acquainted with it.

Posted by Chris Kaminski at 6:42 PM