bas7ex.hlp (Topic list)
Important Notice
The pages on this site contain documentation for very old MS-DOS software, purely for historical purposes. If you're looking for up-to-date documentation, particularly for programming, you should not rely on the information found here, as it will be woefully out of date.
WHILE...WEND statement Programming Example
                       Example                 Contents  Index  Back
──────────────────────────────────────────────────────────────────────────────
'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