For...Next loops
 

The first type of loop we will cover is the For...Next loop. The operation of this type of structure is that it uses a variable to countr from a start value to an end value. The format of this loop is shown below:
For <Variable>=<Expression1> To <Expression2>
Code sequence
Next [ <Variable> ]

The variable must be a numeric type but can be any legal variable name. I do not want to catch any of you callit it "variable" ;). However, it is optional to put it after the Next keyword. Before the start of the loop, the variable will be assigned the value of Expression1. This is now the start of the loop and if the value of the variable is less than or equal to (this can be changed and will be explained below) the value of Expression2, the code sequence inside the loop will be preformed. If the value is greater than Expression2, the program continues executing from the line after the Next keyword associated with the For statement. At the end of the loop, the value of the variable is increased and the program returns to the start of the loop (where it compares the variable to expression2). Here are some examples showing what happens with different values for the expressions:

Code DefType.w i
NPrint "For loop from 0 to 10"
For i=0 To 10
NPrint "Loop 1, counting: ",i
Next i

NPrint "For loop from 5 to 5"
For i=5 To 5

NPrint "Second loop, counting: ",i
Next ; See, no variable name here

NPrint "For loop from 1 to 0"
For i=1 To 0

NPrint "Loop 3, counting: ",i
Next
MouseWait
End

As you will see if you run the program, nothing ever gets printed from the third For...Next loop. This is because the start value of the variable is more than the value of the second expression. This feature can be useful, but what if you want to count backwards? In that case, you would need to use the For...Next loop and include the Step keyword:

For <Variable>=<Expression1> To <Expression2> [ Step <Expression3> ]
Code sequence
Next [ <Variable> ]

This works in the same way as the previous For...Next loops except that instead of increasing the value of the variable at the end of every loop, the value of Expression3 is added to the variable. If the value of Expression3 is negative, the loop will begin if the end value is less than or equal to the start value (which if you remember is the opposite of the original method described previously). So in the previous example (where you needed to count backwards), you could use -1 and it would count down instead of up. Here are some examples of using the Step keyword with For...Next loops.

Code DefType.w i

NPrint "For loop from 5 to 0 with step -1"
For i=5 To 0 Step -1

NPrint "Loop 1, counting: ",i
Next

NPrint "For loop from 0 to 10 with step 3"
For i=0 To 10 Step 3

NPrint "Second loop, counting: ",i
Next

NPrint "For loop from 0 to 2*Pi with fractional step"
For angle.q=0 To 2*Pi-0.1 Step 0.1

; This could be used for e.g. processing a circle using radians instead of
; degrees. The -0.1 in the second expression is so we do not reach the value 2*Pi
; (which would be repeating 0 anyway). Oh, and Pi is a Blitz command which returns
; the mathematical value of Pi (3.141....)
NPrint "Counting fractional values: ",i
Next

NPrint "A little something can count in any direction - change me and see :)"
start.w = 0
end.w = 10
stepsize.w=1
If start > end Then stepsize=-1
For i=start To end Step stepsize

NPrint "Final loop, counting: ",i
Next

MouseWait
End


Previous: Select...End Select decisions Programming contents Next: While...Wend loops


News | Introduction | Search | List/Forum/IRC Webring | Contact
Installation | Troubleshooting | Programming | Archives | Links
Return to programming main
Page last modified: 6th February 2002