Full BASIC does not provide a syntax for escaping from a multi-loop.
Here is secret tricks to escape from multi-loops without using the GOTO statement:
In the case of double loops, if the FOR loop and DO loop are nested, the desired processing can be written by using EXIT FOR and EXIT DO appropriately.
The problem is with the double DO loop and the double FOR loop, but since the FOR loop and the DO loop can be rewritten interchangeably, the above technique can be used by rewriting either the outer or inner loop of the double loop.
In the line following the NEXT statement, EXIT FOR is executed again under the same condition.
This method can also be used to escape from multiple DO-LOOPs.
Example.BR>
Find the smallest Pythagorean integer greater than c0
100 INPUT c0 110 FOR c=c0+1 TO MAXNUM 120 FOR b=1 TO SQR(c)-1 130 LET a=INT(SQR(c-b^2)) 140 IF a^2+b^2 = c THEN EXIT FOR 150 NEXT b 160 IF a^2+b^2 = c THEN EXIT FOR 170 NEXT c 180 PRINT c;"=";a;"^2 +";b;"^2" 190 END
When using this method, it is necessary to confirm that the condition is not satisfied when the NEXT statement is executed to exit the loop, and that the condition will be the same if it is evaluated again.
If you cannot be sure of this, use a flag (a variable used as a switch).
Example.
Find the smallest Pythagorean triple greater than c0
100 INPUT c0 110 LET f=0 120 FOR c=c0+1 TO MAXNUM 130 FOR b=1 TO SQR(c)-1 140 LET a=INT(SQR(c-b^2)) 150 IF a^2+b^2 = c THEN LET f=1 160 IF f=1 THEN EXIT FOR 170 NEXT b 180 IF f=1 THEN EXIT FOR 190 NEXT c 200 PRINT c;"=";a;"^2 +";b;"^2" 210 END
The variable f, which is assigned an initial value of 0 in line 110, is a flag. Write a program to exit the loop when f=1, and change the value of f to 1 when the condition to exit the loop is met.
A FOR...NEXT loop in Full BASIC ends when the value of the control variable exceeds the limit (the number specified in the TO clause of the FOR statement). The value of the control variable is retained even after the loop has finished, so by checking the value of the control variable on the line following the NEXT statement, you can determine whether the loop has stopped when an EXIT FOR statement was executed, or whether it has continued to the end and finished. Therefore, if you determine that the loop has stopped when an EXIT FOR statement was executed, execute the EXIT FOR statement again.
Example.
Find the smallest Pythagorean number greater than c0
100 INPUT c0 110 FOR c=c0+1 TO MAXNUM 120 FOR b=1 TO SQR(c)-1 130 LET a=INT(SQR(c-b^2)) 140 IF a^2+b^2 = c THEN EXIT FOR 150 NEXT b 160 IF b <= SQR(c)-1 THEN EXIT FOR 170 NEXT c 180 PRINT c;"=";a;"^2 +";b;"^2" 190 END
If you use this technique, be careful that the test must be the same numeric value as the limit of the FOR statement. For example, if you specify a condition that includes a variable, you must verify that the value of that variable has not changed within the loop.
To escape from multiple loops, you can write the loop within WHEN EXCEPTION IN 〜 END WHEN and use the technique of generating an exception to escape.
To generate your own exception, use the CAUSE EXCEPTION statement. Exception numbers 1 to 999 are reserved for user use.
Example.
Find the smallest Pythagorean number greater than c0
100 INPUT c0 110 WHEN EXCEPTION IN 120 FOR c=c0+1 TO MAXNUM 130 FOR b=1 TO SQR(c)-1 140 LET a=INT(SQR(c-b^2)) 150 IF a^2+b^2=c THEN CAUSE EXCEPTION 999 160 NEXT b 170 NEXT c 180 USE 190 SELECT CASE EXTYPE 200 CASE 999 210 ! This is normal, so do nothing and skip the exception handling block and proceed. 220 CASE ELSE 220 CASE ELSE 230 EXIT HANDLER ! If an unexpected error occurred, re-raise the exception. 240 END SELECT 250 END WHEN 260 PRINT c;"=";a;"^2+";b;"^2" 270 END
This example shows a double loop, but this technique is effective for any number of loops.
Although not recommended because it often makes the program flow less visible, you can write the loop in an internal subprogram and use EXIT SUB to exit and return.
This is the recommended method if the loop is originally inside a subprogram or function definition.
100 INPUT c0 110 CALL loop1 120 PRINT c;"=";a;"^2+";b;"^2" 130 SUB loop1 140 FOR c=c0+1 TO MAXNUM 150 FOR b=1 TO SQR(c)-1 160 LET a=INT(SQR(c-b^2)) 170 IF a^2+b^2=c THEN EXIT SUB 180 NEXT b 190 NEXT c 200 END SUB 210 END