WhileWend

=While...Wend= toc The **While...Wend** looping structure allows you to rerun a code routine as long as a specified condition evaluates to TRUE. The syntax is as follows:

Syntax: WHILE expr //- Begin a WHILE/WEND loop. Loop if and as long as expr evaluates to true (non zero).// EXIT WHILE //- Quit executing code in the current WHILE/WEND loop and continue with the code following//

The condition is evaluated at the start of the loop. The loop is not executed if the condition does not evaluate to TRUE, so it is possible for a **While...Wend** loop to be skipped. That's the case in the following example. The variable x has no value, so the condition (x = 23) does not evaluate to TRUE and the loop is not executed.

code while x = 23 print x wend code

Simple Loop
Here is a simple loop that initializes the variable **color$** to be "blue". The loop begins and asks the user to type a color. As long as the user types "blue" the loop continues and the user is again asked to type a color. As soon as the user types something other than "blue" the condition //while color$ = "blue"// is no longer true, so the loop is exited.

code format="vbnet" color$ = "blue" print "Current color is ";color$

while color$ = "blue" input "Type a color name."; usercolor$ color$ = lower$(usercolor$) print "You like ";color$ wend

print "You no longer like blue. You now like ";color$ code

Counting
While...Wend loops can be used in a similar way to ForNext loops. The counter variable must be incremented within the loop.

code format="vbnet" x = 0 while x<10 x = x + 1 print "x is ";x wend code

Exit Loop Prematurely
If it is necessary to exit a loop prematurely, this can be done using the **Exit While** statement as follows.

code format="vbnet" x = 0 while x<10 x = x + 1 print "x is ";x if x = 5 then exit while wend code

In Liberty BASIC, we do not jump out of a while...wend loop before the condition evaluates to TRUE. **It is okay to jump out of a loop (for/next and while/wend) in Run BASIC.** Here is Carl's explanation:

//Yes, it is okay to jump out of a FOR NEXT or WHILE WEND loop in Run BASIC using GOTO. This works because of the new way we handle structure at runtime. In Liberty BASIC v4 all the executable objects are in a flat collection, and important stack manipulation is performed by the FOR and NEXT statements, so if you skip over the NEXT statement to get out of the loop then the stack gets messed up. In LB5 and Run BASIC each nested block of execution manages itself. If you GOTO outside of a loop the runtime looks for the branch label progressively to each next outer scope, throwing away each nested block (whether a loop, an IF THEN block, a SELECT CASE block, or whatever) until it finds the branch point. This automatically manages the execution context, and there is nothing else to clean up.//

Expression as Condition
Here is an example where a more complicated expression is evaluated.

code format="vbnet" x = 1 y = 0 while x+y <> 5 y = y + 1 print "y is ";y print "x + y is ";x + y wend print "Done." code

Not

 * Not** can be used in the expression to be evaluated, as in the following example.

code format="vbnet" x = 0 while not(x>9) x = x + 1 print "x is ";x wend code

Nesting
It is possible to nest two or more **While...Wend** loops.

code format="vbnet" x = 1 y = 0 while x < 10 while y < 20 y = y + 1 x = x + 1 print "y is ";y print "x is ";x wend wend print "Done." code

Wait
Unlike Liberty BASIC,a wait statement cannot be used inside a while/wend loop.

code n=1 while n<>10 link #link1, "Next",[go] wait [go] n=n+1 wend code

The code above results in a RB error, Runtime Error: wait Branch label not found: [go]