|
|
xxxxxxxxxxxxxxxxxx |
BASIC LESSON 6
| |||||||||||||||
| 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 |
| 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 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.
| 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.