| .DO...LOOP Statement Programming Examples. |
|
  QuickSCREEN       Details      Example       Contents       Index |
| ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ |
| DO...LOOP Statement Programming Examples |
|
| Following are four examples. The first two examples show you how |
| placement of the condition affects the number of times the block |
| of statements is executed. The third example shows how a loop can |
| be used to pause a program until the user presses a key. The fourth |
| example illustrates testing at the end of a loop and presents |
| a sort subprogram where an ending test is appropriate. |
|   |
| DO...LOOP Programming Example 1 |
|   |
| In the following example, the test is done at the beginning of the |
| loop. Because I is not less than 10, the body of the loop (the |
| statement block) is never executed. |
|   |
| ' DO...LOOP with test at the top of the loop. |
| ' Output shows that loop was not executed. |
| I = 10 |
| PRINT "Value of I at beginning of loop is  ";I |
| DO WHILE I < 10 |
|    I = I + 1 |
| LOOP |
| PRINT "Value of I at end of loop is  ";I |
|   |
| Example 1 Sample Output |
|   |
| Value of I at beginning of loop is  10 |
| Value of I at end of loop is  10 |
|   |
| DO...LOOP Programming Example 2 |
|   |
| The following example tests I at the end of the loop, so the |
| statement block executes at least once. |
|   |
| ' DO...LOOP with test at the bottom of the loop. |
| ' Output shows loop was executed once. |
| I = 10 |
| DO |
|    PRINT "Value of I at beginning of loop is  ";I |
|    I = I + 1 |
| LOOP WHILE I < 10 |
| PRINT "Value of I at end of loop is  ";I |
|   |
| Example 2 Sample Output |
|   |
| Value of I at beginning of loop is  10 |
| Value of I at end of loop is 11 |
|   |
| DO...LOOP Programming Example 3 |
|   |
| In the following example, the DO...LOOP continues testing the length |
| of Choice$ at the bottom of the loop.  When the user presses a key, |
| the length of Choice$ becomes greater than zero and the loop terminates. |
|   |
| ' DO...LOOP with test at the bottom of the loop. |
| DO |
|    Choice$ = INKEY$ |
| LOOP WHILE Choice$ = "" |
|   |
| DO...LOOP Programming Example 4 |
|   |
| The following sort program 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. |
|   |
| ' Set up a special value to indicate no exchanges. |
| ' |
| CONST NOEXCH = -1 |
| DIM Exes(12) |
| FOR I = 1 TO 12 |
|    Exes(I) = 13 - I |
| NEXT I |
| Limit = 12 |
| CLS                                  'Clear the screen |
| PRINT "This is the list of numbers to sort:" |
| FOR I = 1 TO 12 |
|    PRINT Exes(I); |
| NEXT I |
| LOCATE 4,1: INPUT "Press any key to continue", Gar$ |
| 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 |
|    Limit = Exchange                  'Sort on next pass only to where |
|                                      'last exchange was done. |
| LOOP UNTIL Exchange = NOEXCH         'Sort until no elements are exchanged. |
| ' |
| PRINT |
| PRINT "Sorting is completed. This is the sorted list:" |
| FOR I = 1 TO 12 |
|    PRINT Exes(I); |
| NEXT I |
| END |
|   |
| Sample Output |
|   |
| This is the list of numbers to sort: |
| 12  11  10  9  8  7  6  5  4  3  2  1 |
|   |
| Press any key to continue |
|   |
| This is the sorted list: |
| 1  2  3  4  5  6  7  8  9  10  11  12 |