Liberty BASIC Help Online

WHILE...[EXIT WHILE]...WEND
 
WHILE expression
  {some code}
WEND
 
Description:
These two statements comprise the start and end of a control loop.  Between the WHILE and WEND code is placed (optionally) that is executed repeatedly while expression evaluates to true.  The code between any WHILE statement and its associated WEND statement will not execute even once if the WHILE expression initially evaluates to false.  Once execution reaches the WEND statement, for as long as the WHILE expression evaluates to true, then execution will jump back to the WHILE statement.  "Expression" can be a boolean, numeric, or string expression or combination of expressions.
 
Usage:
 
  ' loop until midnight (go read a good book)
  while time$ <> "00:00:00"
      ' some action performing code might be placed here
  wend
 
Or:
 
  ' loop until a valid response is solicited
  while val(age$) = 0
     input "How old are you?"; age$
     if val(age$) = 0 then print "Invalid response.  Try again."
  wend
 
Note: A program SHOULD NOT exit a WHILE...WEND loop using GOTO.  It may cause the program to behave unpredictably.  (See EXIT WHILE, below.)
 
GOSUB, FUNCTION and SUB may be used within a WHILE...WEND loop because they only temporarily redirect program flow or call on other parts of the program. Program execution resumes within the WHILE/WEND loop in these instances.  Program execution does not return to the WHILE/WEND loop if GOTO is used within the loop.  GOTO should not be used to exit a WHILE/WEND loop.  EXIT WHILE will correctly exit the loop before it would have terminated normally.

The following example is an example of a WHILE...WEND loop exited improperly:

  while count < 10
    input "Enter a name (or a blank line to quit) ?"; n$
    if n$ = "" then [exitLoop]
    list$(count) = n$
    count = count + 1
  wend
[exitLoop]
 
Instead, use the EXIT WHILE statement:
 
  while count < 10
    input "Enter a name (or a blank line to quit) ?"; n$
    if n$ = "" then EXIT WHILE
  wend
 
[exitLoop]
  print "Done!"
 


Copyright (C) 2003 Shoptalk Systems
Liberty BASIC - http://www.libertybasic.com/