Q(uick)BASIC Statement: DO...LOOP

Quick View

DO...LOOP

A control flow statement that repeats a block of statements while a condition is true or until a condition becomes true

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • DO [{WHILE | UNTIL} condition][statementblock]LOOP
  • DO[statementblock]LOOP [{WHILE | UNTIL} condition]
Description/Parameter(s)
condition A numeric expression that Basic evaluates as true (nonzero) or false (zero).
Example
i% = 0 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%
Syntax
  • DO [{WHILE | UNTIL} booleanexpression][statementblock]LOOP
  • DO[statementblock]LOOP [{WHILE | UNTIL} booleanexpression]
Description/Parameter(s)
Argument Description
statementblock One or more BASIC statements to be repeated
booleanexpression Any expression that evaluates to true (nonzero) or false (zero)

You can use a DO...LOOP statement instead of a WHILE...WEND statement. The DO...LOOP is more versatile because it can test for a condition at the beginning or at the end of a loop.

Examples

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

Sample Output:

Value of I at beginning of loop is 10 Value of I at end of loop is 10

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

Sample Output:

Value of I at beginning of loop is 10 Value of I at end of loop is 11

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$ = ""

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
Syntax
  • DO [{WHILE | UNTIL} condition][statementblock][EXIT DO][statementblock]LOOP
  • DO[statementblock][EXIT DO][statementblock]LOOP [{WHILE | UNTIL} condition]
Description/Parameter(s)
  • EXIT DO is an alternative exit from a DO...LOOP. EXIT DO transfers control to the statement following the LOOP statement. When used within nested DO...LOOP statements, EXIT DO transfers out of the immediately enclosing loop. EXIT DO can be used only in a DO...LOOP statement.

Usage Notes

  • You can use a DO...LOOP statement instead of a WHILE...WEND statement. The DO...LOOP is more versatile because it can test for a condition at the beginning or at the end of a loop.

Example

These three examples use DO...LOOP to show how the placement of the condition affects the number of times the block of statements is executed, to illustrate testing at the end of a loop, and to present a SUB procedure where an ending test is appropriate.

DIM I AS INTEGER CLS 'In this 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 "Example 1:": PRINT 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 '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 PRINT : PRINT : PRINT "Example 2:": PRINT 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 '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) 'Load the array and mix it up. FOR I = 1 TO 12 STEP 2 Exes(I) = 13 - I Exes(I + 1) = 0 + I NEXT I Limit = 12 PRINT : PRINT : PRINT "Example 3:": PRINT 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 'In the following DO...LOOP, INKEY$ is tested at the bottom of the loop. 'When the user presses a key, INKEY$ is no longer a null string and the 'loop terminates, continuing program execution. 'DO...LOOP with test at the bottom of the loop. PRINT : PRINT "Press any key to continue." DO 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 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:": PRINT FOR I = 1 TO 12 PRINT USING " ### "; Exes(I); NEXT I END