Small Basic > Getting Started Guide > Chapter 5: Loops

### For Loop

Let’s take a program we wrote in the previous chapter.

``````i = 1
start:
TextWindow.WriteLine(i)
i = i + 1
If (i < 25) Then
Goto start
EndIf``````

This program prints out numbers from 1 to 24 in order. This process of incrementing a variable is very common in programming that programming languages usually provide an easier method of doing this. The above program is equivalent to the program below:

``````For i = 1 To 24
TextWindow.WriteLine(i)
EndFor``````

And the output is:

Figure 5.1 - Using the For Loop

Notice that we’ve reduced the 8 line program to a 4 line program, and it still does exactly the same as the 8 line program! Remember earlier we said that there are usually several ways of doing the same thing? This is a great example.

For..EndFor is, in programming terms, called a loop. It allows you to take a variable, give it an initial and an end value and let the computer increment the variable for you. Every time the computer increments the variable, it runs the statements between For and EndFor.

But if you wanted the variable to be incremented by 2 instead of 1 – like say, you wanted to print out all the odd numbers between 1 and 24, you can use the loop to do that too.

``````For i = 1 To 24 Step 2
TextWindow.WriteLine(i)
EndFor``````

Figure 5.2 - Just the Odd Numbers

The Step 2 part of the For statement tells the computer to increment the value of i by 2 instead of the usual 1. By using Step you can specify any increment that you want. You can even specify a negative value for the step and make the computer count backwards, like in the example below:

``````For i = 10 To 1 Step -1
TextWindow.WriteLine(i)
EndFor``````

Figure 5.3 - Counting Backwards

### While Loop

The While loop is yet another looping method, that is useful especially when the loop count is not known ahead of time. Whereas a For loop runs for a pre-defined number of times, the While loop runs until a given condition is true. In the example below, we’re halving a number until the result is greater than 1.

``````number = 100
While (number > 1)
TextWindow.WriteLine(number)
number = number / 2
EndWhile``````

Figure 5.4 - Halving Loop

In the program above, we assign the value 100 to number and run the while loop as long as number is greater than 1. Inside the loop, we print out the number and then we divide it by two, effectively halving it. And as expected, the output of the program is numbers that are progressively getting halved one after another.

It’ll be really hard to write this program using a For loop, because we don’t know how many times the loop will run. With a while loop it’s easy to check for a condition and ask the computer to either continue the loop or quit.

It’ll be interesting to note that every while loop can be unwrapped into an If..Then statement. For instance, the program above can be rewritten as follows, without affecting the end result.

``````number = 100
startLabel:
TextWindow.WriteLine(number)
number = number / 2
If (number > 1) Then
Goto startLabel
EndIf``````

Note: In fact, the computer internally rewrites every While loop into statements that use If..Then along with one or more Goto statements.

Next Chapter