Back to Gambas
Introduction to loops
Loops are powerful control structures used to repeat a given section of code a certain number of times or until a particular condition is met.
Gambas has three main types of loops: for / next loops , do loops and for / each loops.
For / Next Loops
The syntax of a for / next loop has three components:
- a counter,
- a range, and
- a step.
A basic for / next loop appears as follows:
for X = 1 to 100 step 1 print X next
In this example, X is the counter, "1 to 100" is the range, and "1" is the step.
When you want to make a complete example out of that , you need a form and a command button and this following code.
PUBLIC SUB Button1_Click() X AS Integer for X = 1 to 100 step 1 print X next END
The variable X has to be declared as an integer.
When a for / next loop is initialized, the counter is set to the first number in the range—in this case, X is set to 1. The program then executes any code between the for and next statements normally. Upon reaching the next statement, the program returns to the for statement and increases the value of the counter by the step. In this instance, X will be increased to 2 on the second iteration, 3 on the third, etc.
To change the amount by which the counter variable increases on each iteration, simply change the value of the step. For example, if you use a step 2, X will increase from 1 to 3, then to 5, 7, 9, and so on. When the step is not explicitly stated, step 1 is used by default. (Note that the step can be a negative value. For instance, "for X = 100 to 1 step -1" would decrease the value of X from 100 to 99 to 98, etc.)
When X reaches the maximum value in the range (100 in the example above), the loop will cease to execute, and the program will continue to the code beyond the next statement.
It is possible to edit the value of the counter variable within a for / next loop, although this is generally considered bad programming practice:
for X = 1 to 100 step 1 print X X = 7 next
While you may on rare occasions find good reasons to edit the counter in this manner, the example above illustrates one potential pitfall: Because X is set to 7 at the end of every iteration, this code creates an infinite loop. To avoid this and other unexpected behavior, use extreme caution when editing the counter variable!
Try some more examples to get used to loops:
PUBLIC SUB Button1_Click() i AS Integer FOR i = 1 TO 10 PRINT "Hallo Welt" NEXT END
PUBLIC SUB Button1_Click() DIM i AS Integer FOR i = 1 TO 10 PRINT i,i*i,i*i*i NEXT END
To make loops better readable, you should move the inner lines of code to the right. They will be repeated in the loop. In contrary to VB you should not mention the counter at the end of the loop behind the next command.
For I = 1 to 10 print I ' Next I VB code ' skipped in Gambas because it is redundant Next
Do loops are a bit more flexible then For loops, but should generally only be used when neccesary. Do loops come in the following formats:
While loops (both do while and loop while) will continue to execute as long as a certain conditional is true. An Until loop will loop as long as a certain condition is false, on the other hand. The only difference between putting either While or Until in the Do section or the Loop section, is that Do checks when the loop starts, and Loop check when the loops ends. An example of a basic loop is as follows:
Do print "hello" x=x+1 Loop until x=10
This loop will print hello several times (depending on the initial value of x. As you may have noticed, Do loops have no built in counters. However, they may be made manually as shown above. In this case, I chose x as my counter variable, and every time the loop execute, x increase itself by one. When X reaches 10, the loop will cease to execute. The advantage of Do loops is that you may exit at any time whenever any certain conditional is met. You may have it loop as long as a certain variable is false, or true, or as long as a variable remains in a certain range.
A nested loop is any type of loop inside an already existed loop. They can involve any type of loop. For this, we will use For loops. It is important to remember that the inside loop will execute its normal amount multiplied by how many times the exterior loop runs. For example:
For i = 1 to 10 For j = 1 to 10 print i*j, Next print Next
This will print the times table. Upon the first pass of the i loop, it will run the j loop ten times. Then on the second pass of the i loop, it will run the j loop another ten times, and so on. Why is there another print command between the two next commands. Try it out, what you will get, when you delete it.