Copyright 2008 Shoptalk Systems

WHILE/WEND and [EXIT WHILE]

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. The WHILE expression can be a boolean or numeric expression.

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

GOSUB and CALL 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.

GOTO can  be used to exit a WHILE/WEND loop, but GOTO cannot be used to jump into a WHILE/WEND loop.

EXIT WHILE

Use of EXIT WHILE will correctly exit the loop before it would have terminated normally.

Example:

  while count < 10
    input "Enter a name (or a blank line to quit) ?"; n$
    if n$ = "" then exit while
  wend

[exitLoop]
  print "Done!"