While...Wend loops
 

The second type of loop to be covered here is the While...Wend and has a format like this:
While <Expression>
Code sequence
Wend

The operation is that while the value of the expression is true, the inner code sequence is performed. More specifically, at the start of each loop the value of the expression is calculated and if it is true, the inner code sequence is performed and at the end of the loop the program simply goes back to the start of the loop (the expression comparison). If the value of the expression was false, the program continues from the line after the Wend. An example of using While...Wend loops is shown below:

Code ; A list array will be processed using a while loop - first create some items
Dim List name$(10)
If AddItem(name$()) Then name$()="Frank"
If AddItem(name$()) Then name$()="Jim"
If AddItem(name$()) Then name$()="Dave"
If AddItem(name$()) Then name$()="Bob"
If AddItem(name$()) Then name$()="John"
If AddItem(name$()) Then name$()="Kevin"
If AddItem(name$()) Then name$()="Chris"

ResetList name$()
While NextItem(name$())

; Remember from the Arrays and Lists topic that the NextItem() command
; returns True if it could move onto the next item
NPrint "Current name in list: ",name$()
Wend

; And now a simpler loop which uses a variable to count DefType.w i
i = 0
While i<10

NPrint "Simple loop, counting: ",i
i+1 ; Remember from the variables topic this is a quick way to add 1 to a variable
Wend

; This shows when the comparisons are done in while loops i = 10
While i<0

NPrint "Loop3, counting: ",i
i+1
Wend

MouseWait
End

And that is pretty much all there is to while loops. As you can see in the third example, the loop is never entered because the value of the loop expression is calculated before the first time round the loop. Since it is false in that example, the loop is never processed.

In the first example, the loop was able to continue because the command NextItem returns true when it is able to move onto the next item in the list (from the Arrays and lists topic. The same goes for the AddItem commands used at the top of the program.


Previous: For...Next loop Programming contents Next: Repeat...Until loops


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