DO...LOOP Statement Example
◄Example► ◄Contents► ◄Index► ◄Back►
──────────────────────────────────────────────────────────────────────────────
' This example uses a DO...LOOP statement to perform tests at the end of the
' loop because the entire array must be examined at least once to see if
' it is in order. In general, test at the end of a loop only if you know
' that you always want the statement block executed at least once.
' To try this example:
' 1. Choose New Project from the File menu
' 2. Copy the code example below to the code window
' 3. Press F5 to run the example
CONST NOEXCH = -1 ' Set up a special value to indicate no exchanges
DIM Exes(12)
CLS ' Clear the screen
FOR i = 1 TO 12 STEP 2 ' Load the array and mix it up
Exes(i) = 13 - i
Exes(i + 1) = 0 + i
NEXT i
Limit = 12
PRINT "This is the list of numbers to sort in ascending order:"
PRINT
FOR i = 1 TO 12
PRINT USING " ### "; Exes(i);
NEXT i
PRINT
PRINT : PRINT "Press any key to continue..."
DO ' DO...LOOP with test at the bottom of the loop
LOOP WHILE INKEY$ = ""
DO
Exchange = NOEXCH
FOR i = 1 TO Limit - 1 ' Make one pass over the array
IF Exes(i) > Exes(i + 1) THEN
SWAP Exes(i), Exes(i + 1) ' Exchange array elements
Exchange = i ' Record location of most
END IF ' recent exchange
NEXT i ' Sort on next pass only to where
Limit = Exchange ' last exchange was done
LOOP UNTIL Exchange = NOEXCH ' Sort until no elements are exchanged
PRINT : PRINT "Sorting is completed. This is the sorted list:": PRINT
FOR i = 1 TO 12
PRINT USING " ### "; Exes(i);
NEXT i
END