Calculus Computer Lab
Math 190


News

Lab Schedule

QuickBasic Notes

Fortran Notes

Tutor Room

Winplot Manual

Lab Home

xxxxxxxxxxxxxxxxxx

BASIC LESSON 6

Lesson Topics
DO WHILE ... LOOPDO ... LOOP UNTIL
DO ... LOOP WHILEEXIT (from Loops)
DO UNTIL ... LOOP
View DemosDownload Demos
# 1 # 2 # 3 # 4 # 5 # 6 # 7 # 1 # 2 # 3 # 4 # 5 # 6 # 7
Main Basic Page

Besides the FOR … NEXT loop and the GOTO … LABEL construction, in Basic there are several other ways to do loops. In most programming problems where a loop is required, there will be more than one way you can do the loop - you should choose the one you find most convenient for that particular problem.

DO WHILE ... LOOP

This loop takes the form

DO WHILE condition
instructions
LOOP

Basic first checks the condition - if it is satisfied, then the instructions are carried out. When Basic reaches the bottom LOOP statement, it returns to the top to the DO statement and repeats. If at any time at the top of the loop the condition is not satisfied, then Basic jumps out of the loop and to the next statement in the program after the loop. The following sequence adds the integers from 1 to 10:

N = 1
S = 0
DO WHILE N < 11
S = S + N
N = N + 1
LOOP

The first time through the loop the instructions are carried out, since N = 1 < 11. Note that S changes to 0 + 1 = 1, and N to 1 +1 = 2. The instructions are carried out also the second time through the loop, when S changes to 1 + 2 = 3 and N to 2 + 1 = 3. The loop continues until N reaches 11, at which time the condition N < 11 is no longer satisfied and the loop stops; then the final value of S is the sum of the integers from 1 to 10, and Basic continues to the next statement in the program.

DO ... LOOP WHILE

This loop is similar to the DO WHILE ... LOOP, except that the condition is checked at the bottom of the loop. The form of the loop is

DO
instructions
LOOP WHILE condition

The first time through the loop, Basic carries out the instructions. At the bottom of the loop Basic checks the condition, and if it is satisfied Basic goes back to DO and repeats. If at any time the condition is not satisfied, Basic stops looping and goes on to the next statement. The following sequence likewise adds the integers from 1 through 10:

N = 1
S = 0
DO
S = S + N
N = N + 1
LOOP WHILE N < 11

DO UNTIL ... LOOP and DO ... LOOP UNTIL

These loops take the respective forms

DO UNTIL condition DO
instructions instructions
LOOP LOOP UNTIL condition

Basic checks the condition, and if it is not satisfied then the loop continues. The loop repeats until the condition is satisfied, at which point Basic jumps from the loop and to the next statement in the program. The following sequences demonstrate still two more ways to add the integers from 1 through 10:

N = 1 : S = 0 N = 1 : S = 0
DO UNTIL N > 10 DO
S = S + N S = S + N
N = N + 1 N = N + 1
LOOP LOOP UNTIL N > 10

In the left example Basic tests at the top of the loop, and in the right example at the bottom. In each case the loop stops when N reaches 11.

EXIT (from Loops)

The EXIT command, followed by the loop type, is used to jump out of a loop. The command EXIT DO jumps Basic out of a DO … LOOP to the next statement following the loop, while EXIT FOR jumps Basic from a FOR … NEXT loop. Usually EXIT is part of an IF … THEN construction, instructing Basic to exit only under a certain condition. Here is an example of EXIT FOR:

FOR I = 1 TO 100
INPUT "Enter a name, or 'n' if you are out of names:", stu$(I)
IF stu$(I) = "n" THEN
PRINT "OK - so you are out of names"
EXIT FOR
END IF
NEXT I

The user, after running out of student names, enters "n" so that Basic stops prompting and jumps out of the loop. As long as another name is typed, the loop continues, up to a maximum of 100 names.

Here is an example of EXIT DO:

INPUT "What is your positive integer "; N
K = 2
root = SQR(N)
DO WHILE K <= root
IF N MOD K = 0 THEN
PRINT "Your integer is not a prime number."
EXIT DO
END IF
K = K + 1
LOOP

Observe that if N has a divisor K, so that N MOD K = 0, then at that value of K the program announces that N is not a prime number and exits the loop. If the program makes it all the way through the loop with no exit, then N necessarily is a prime number.