Loops


Loops

If you want to do something over and over again you could just repeat the same code a number of times. Let us look at an example:

'This program asks the user to type in a number and
'prints out the square of that number.
'This is repeated five times.


input "Type a number: "; number
print "The square of "; number; " is "; number^2
input "Type a number: "; number
print "The square of "; number; " is "; number^2
input "Type a number: "; number
print "The square of "; number; " is "; number^2
input "Type a number: "; number
print "The square of "; number; " is "; number^2
input "Type a number: "; number
print "The square of "; number; " is "; number^2


The run of the program would look like this:


Notice that the same thing is repeated 5 times, but the user typed in 5 different numbers.

Even though repeating the code in this manner works, it is very inefficent as it is both a waste of memory and time and not adequate for all situations. A better way to do it is with a loop. A loop is a way to create repetition in your program. In your program you only need to write one section of code and place it within a loop. The loop will continue to repeat the code until a certain condition is met. In Liberty BASIC there are two types of loops: For Loops and While Loops.

For Loops

A For Loop allows you to specify a certain number of times to repeat your code. Each time through the loop is called an iteration. Therefore if your loop repeats 10 times it runs for 10 iterations. In a For Loop you use a counter variable. A counter variable is a variable which keeps track of what iteration your loop is on.

Even though a variable can have almost any name, most programmers call the counter variable: i or counter. If you have "nested" For Loops (For Loops inside of For Loops) in your program the counter variables are generally called i, j, k, etc.

Let us rewrite the above program so that we use a For Loop.

'This program asks the user to type in a number and
'prints out the square of that number.
'This is repeated five times.


for i = 1 to 5
input "Type a number: "; number
print "The square of "; number; " is "; number^2
next i


This program produces the exact same run as the previous program did. However, this program is much more efficient. If you wanted to repeat this code 100 times for the previous program you would have had to repeat the same code 100 times. Using a For Loop you only need to write the code once and simply change the number of times from 5 to 100.

How the For Loop Works

The For Loop is given three items: Using the above example, when the loop is first started the starting number (1) gets stored in the counter variable (i). When the loop reaches the last statement (next i) it increments i by 1. In other words the number 2 is now stored in i. The program will then return to the first line inside of the loop. The loop will repeat three more times, setting i to 3, then 4, then 5. When i is 5 and reaches the end of the loop it once again reads the "next i" statement. Since 5 is the ending number there is no "next i" and therefore the loop is finished.

You can also use the counter variable inside your loop for a special purpose. For example, if you wanted to print the numbers from 1 to 10 you would use the following code:

'This program prints the numbers from 1 to 10

for i = 1 to 10
print i
next i


The run would look like this:


Maybe you want to create a program that will print the multiples of 10. You could create a program like this:

'This program prints the multiples of 10

print "The multiples of 10"
print "==================="
for i = 1 to 10
print i * 10
next i


The run would look like this: