Q(uick)BASIC Statement: WHILE...WEND
Quick View
WHILE...WEND
A control flow statement that executes a series of statements in a loop, as long as a given condition is true
Worth knowing
Useful and cross-version information about the programming environments of QBasic and QuickBasic.
Syntax
- WHILE condition
⋮
WEND
Description/Parameter(s)
condition | A numeric expression that Basic evaluates as true (nonzero) or false (zero). |
DO...LOOP provides a better way to execute statements in a program loop. |
See also:
Syntax
- WHILE condition [statements] WEND
Description/Parameter(s)
If the condition is true (that is, if it does not equal zero), then any intervening statements are executed until the WEND statement is encountered. BASIC then returns to the WHILE statement and checks condition. If it is still true, the process is repeated. If it is not true (or if it equals zero), execution resumes with the statement following the WEND statement.
Note: | QuickBASIC's DO...LOOP statement provides a more powerful and flexible loop control structure. |
WHILE...WEND loops may be nested to any level. Each WEND matches the most recent WHILE. An unmatched WHILE statement causes an error message that reads "WHILE without WEND." An unmatched WEND statement causes an error message that reads "WEND without WHILE."
Note: | Do not branch into the body of a WHILE...WEND loop without executing the WHILE. This may cause run-time errors or program problems that are difficult to locate. |
Example
This example performs a bubble sort on the array A$. Assigning the variable Exchange a nonzero value makes it true, forcing one pass through the WHILE...WEND loop (this construction is unnecessary with DO...LOOP). When there are no more swaps, all elements of A$ are sorted, Exchange is false (equal to zero), and the program continues execution with the line following WEND.
' Bubble sort of array A$.
CONST FALSE=0, TRUE=NOT FALSE
DIM A$(4)
A$(1) = "New York"
A$(2) = "Boston"
A$(3) = "Chicago"
A$(4) = "Seattle"
Max = UBOUND(A$)
Exchange=TRUE ' Force first pass through the array.
WHILE Exchange ' Sort until no elements are exchanged.
Exchange=FALSE
' Compare the array elements by pairs. When two are exchanged,
' force another pass by setting Exchange to TRUE.
FOR I = 2 TO Max
IF A$(I-1) > A$(I) THEN
Exchange = TRUE
SWAP A$(I - 1), A$(I)
END IF
NEXT
WEND
CLS
FOR I = 1 TO 4
PRINT A$(I)
NEXT I
END
Sample Output:
Boston Chicago New York SeattleSee also:
Syntax
- WHILE condition
⋮
WEND
Description/Parameter(s)
condition | A numeric expression that BASIC evaluates as true (nonzero) or false (zero). |
The program lines between the WHILE and WEND statements are executed as long as condition is true. |
Usage Notes
- If condition is true (that is, if it does not equal zero), then any intervening statements are executed until the WEND statement is encountered. BASIC then returns to the WHILE statement and checks the condition. If it is still true, the process is repeated. If it is not true (or if it equals zero), execution resumes with the statement following the WEND statement.
- WHILE...WEND loops can be nested to any level. Each WEND matches the most recent WHILE. When BASIC encounters an unmatched WHILE statement, it generates the error message, "WHILE without WEND." If BASIC encounters an unmatched WEND statement, it generates the error message, "WEND without WHILE."
- Do not branch into the body of a WHILE...WEND loop without executing the WHILE. This can cause run-time errors or program problems that are difficult to locate.
- BASIC's DO...LOOP statement provides a more powerful and flexible loop control structure.
Example
This example uses the WHILE...WEND statement to perform a bubble sort.
'Bubble sort of array A$.
CONST FALSE = 0, TRUE = -1
DIM I AS INTEGER
DIM A$(4)
A$(1) = "New York"
A$(2) = "Boston"
A$(3) = "Chicago"
A$(4) = "Seattle"
Max = UBOUND(A$)
Exchange = TRUE 'Force first pass through the array.
WHILE Exchange 'Sort until no elements are exchanged.
Exchange = FALSE
'Compare the array elements by pairs. When two are exchanged,
'force another pass by setting Exchange to TRUE.
FOR I = 2 TO Max
IF A$(I - 1) > A$(I) THEN
Exchange = TRUE
SWAP A$(I - 1), A$(I)
END IF
NEXT
WEND
CLS
FOR I = 1 TO 4
PRINT A$(I)
NEXT I
END
See also: