WHILE/WEND statements

Purpose

Define a block of program statements that are executed repeatedly for as long as certain conditions are met.

Syntax

WHILE integer_expression

  {statements}

  [EXIT LOOP]

  {statements}

WEND

Remarks

If integer_expression is TRUE (it evaluates to a non-zero value), all of the statements between the WHILE and the terminating WEND are executed.  PowerBASIC then jumps back to the WHILE statement and repeats the test.  If it is still TRUE, PowerBASIC executes the enclosed statements again.  This process is repeated until the test expression evaluates to zero, or an EXIT statement is encountered.  In either case, execution passes to the statement following WEND.

If integer_expression evaluates to FALSE (zero) on the first pass, none of the statements in the loop are executed.

Loops built with WHILE/WEND statements can be nested (enclosed within each other).  Each WEND matches the most recent unmatched WHILE.

One use of a WHILE/WEND loop is to input data from a file until the end of the file is reached:

i& = 0

WHILE ISFALSE EOF(1)

  INCR i&

  LINE INPUT #1, FileTxt$(i&)

WEND

Although the compiler does not care, it's a good idea to indent the statements between WHILE and WEND, to clarify the structure of the loop you have constructed.

Note that the following code creates in infinite loop:

WHILE -1

  ...

WEND

To exit a WHILE/WEND loop prematurely, use the EXIT LOOP statement.

PowerBASIC's DO/LOOP construct offers a more flexible way to build conditional loops.

Also see the discussion on the IF statement for notes on PowerBASIC's Short-circuit evaluation and its possible side effects.

See also

DO/LOOP, EXIT, FOR/NEXT, ITERATE, Short-circuit evaluation