DO ~ LOOP ☆☆☆

On a DO ~ LOOP block, each statement written between the DO-line and the LOOP-line is executed repeatedly.

Example 1.

10 ! Repeats infinitely
20 LET a=0
30 DO
40    PRINT a
50    LET a=a+1
60 LOOP
70 END



DO WHILE
When a WHILE-clause is written in the DO-line, each statement written between the DO-line and the LOOP-line is executed repeatedly while the condition(logical expression)written in the WHILE-clause is true. If the condition is false from the first, no statement between the DO-Line and the LOOP-line is executed.
Example 2.

10 INPUT n
20 LET i=1
30 DO WHILE i<=n
40    PRINT i
50    LET i=i+1
60 LOOP
70 END



DO UNTIL
When a UNTIL-clause is written in the DO-line, each statement written between the DO-line and the LOOP-line is executed repeatedly while the condition written in the UNTIL-clause is false. If the condition is true from the first, no statement between the DO-Line and the LOOP-line is executed.
Example 3 (equivalent to Example 2).

10 INPUT n
20 LET i=1
30 DO UNTIL i>n
40    PRINT i
50    LET i=i+1
60 LOOP
70 END



LOOP WHILE
When a WHILE-clause is written in the LOOP-line, after executing each statement between the DO-Line and the LOOP-line, the condition written in the WHILE-clause shall be tested. If the result is true, the control goes back to the DO-Line.
Example 4.

10 INPUT n
20 LET i=1
30 DO 
40    PRINT i
50    LET i=i+1
60 LOOP WHILE i<=n
70 END

If a number greater than 1 is entered, Example 1 and Example 4 act as the same, but if 0 is entered, the 40-line and the 50-line of Example 2 never be executed, while the 40-line and the 50-line of Example 4 shall be executed once respectively.

LOOP UNTIL
When a UNTIL-clause is written in the LOOP-line, after executing each statement between the DO-Line and the LOOP-line, the condition written in the WHILE-clause shall be tested. If the result is false, the control goes back to the DO-Line.
Example 5 (equivalent to Example 4).

10 INPUT n
20 LET i=1
30 DO
40    PRINT i
50    LET i=i+1
60 LOOP UNTIL i>n
70 END



EXIT DO
If a EXIT DO statement is executed, the control branches to the next line of the LOOP-line.
Example 6.

10 INPUT n
20 LET i=1
30 DO 
40    PRINT i
50    IF i>=n THEN EXIT DO
60    LET i=i+1
70 LOOP
80 END



An EXIT DO statement can be written in a DO~LOOP block. If EXIT DO statement has two or more DO~LOOP blocks that include it, the execution of it means the escape from the most inner DO-block. For example, if the EXIT DO statement in the 50-line is executed, the control branches to the 80-line on the following program.

10 DO
20   ……
30    DO
40       ……
50       EXIT DO
60       ……
70    LOOP
80    ……
90 LOOP
100 ……

If you want to switch the control to the 100-line from the location of the 50-line, use a GOTO statement.