BASIC Flow Control Statements - fvdhoef/aquarius-plus GitHub Wiki
CONT
TYPE: BASIC Flow Control Command
FORMAT: CONT
Action:
This command re-starts the execution of a program which was halted by a STOP
or END
statement or the RUN/STOP
key being pressed. The program will re-start at the exact place from which it left off.
While the program is stopped, the user can inspect or change any variables or look at the program. When debugging or examining a program,
STOP
statements can be placed at strategic locations to allow examination of variables and to check the flow of the program.
The ?CN error message will result from editing the program or if the program halted due to an error, or if you caused an error before typing CONT
to re-start the program.
Examples:
10 PI=0:C=1
20 PI=PI+4/C-4/(C+2)
30 PRINT PI
40 C=C+4:GOTO 20
This program calculates the value of PI. RUN
this program, and after a short while hit the RUN/STOP
key. You will see the display:
BREAK IN 20
NOTE: Might be different number.
Type the command PRINT C
to see how far the Aquarius has gotten. Then use CONT
to resume from where the Aquarius left off.
END
TYPE: BASIC Flow Control Statement
FORMAT: END
Action:
This finishes a program's execution and displays the Ok
message, returning control to the person operating the computer. There may be any number of END
statements within a program. While it is not necessary to include any END
statements at all, it is recommended that a program does conclude with one, rather than just running out of lines.
The END
statement is similar to the STOP
statement. The only difference is that STOP causes the computer to display the message BREAK IN LINE XX
and END
just displays Ok
. Both statements allow the computer to resume execution by typing the CONT
command.
EXAMPLES of END Statement:
10 PRINT"DO YOU REALLY WANT TO RUN THIS PROGRAM"
20 INPUT A$
30 IF A$ = "NO" THEN END
40 REM REST OF PROGRAM . . .
999 END
NEXT
FOR ... TO ... [STEP] ...TYPE: BASIC Flow Control Statement
FORMAT: FOR variable = start TO limit [ STEP increment ]
Action:
This is a special BASIC statement that lets you easily use a variable as a counter. You must specify certain parameters: the floating-point variable name, its starting value, the limit of the count, and how much to add during each cycle.
Here is a simple BASIC program that counts from 1 to 10, PRINTing each number and ENDing when complete, and using no FOR
statements:
100 L = 1
110 PRINT L
120 L = 1 + 1
130 IF L <= 10 THEN 110
140 END
Using the FOR
statement, here is the same program:
100 FOR L = 1 TO 10
110 PRINT L
120 NEXT L
130 END
As you can see, the program is shorter and easier to understand using the FOR
statement.
When the FOR
statement is executed, several operations take place. The start value is placed in the variable being used in the
counter. In the example above, a I is placed in L.
When the NEXT
statement is reached, the increment value is added to the variable. If a STEP
was not included, the increment is set to +1. The first time the program above hits line 120, 1 is added to L, so the new value of L is 2.
Now the value in the variable is compared to the limit. If the limit has not been reached yet, the program G0es TO the line after the original FOR statement. In this case, the value of 2 in L is less than the limit of 10, so it GOes TO line 110.
Eventually, the value of limit is exceeded by the variable. At that time, the loop is concluded and the program continues with the line
following the NEXT
statement. In our example, the value of L reaches 11, which exceeds the limit of 10, and the program goes on with line
130.
When the value of increment is positive, the variable must exceed the limit, and when it is negative it must become less than the limit.
NOTE: A loop always executes at least once.
EXAMPLES of FOR...TO...STEP... Statement:
100 FOR L = 100 TO 0 STEP -1
100 FOR L = PI TO 6* {pi} STEP .01
100 FOR AA = 3 TO 3
GOSUB
TYPE: BASIC Flow Control Statement
FORMAT: GOSUB line_number
FORMAT: GOSUB label
Action:
This is a specialized form of the GOTO
statement, with one important difference: GOSUB
remembers where it came from. When the
RETURN
statement (different from the RETURN
key on the keyboard) is reached in the program, the program jumps back to the statement
immediately following the original GOSUB
statement.
The major use of a subroutine (GOSUB
really means GO to a SUBroutine) is when a small section of program is used by different sections of the program. By using subroutines rather than repeating the same lines over and over at different places in the program, you can save lots of program space. In this way, GOSUB
is similar to DEF FN
. DEF FN
lets you save space when using a formula, while GOSUB saves space when using a several-line routine. Here is an inefficient program that doesn't use GOSUB
:
EXAMPLES of GOSUB Statement:
100 PRINT "THIS PROGRAM PRINTS"
110 FOR L = 1 TO 500:NEXT
120 PRINT "SLOWLY ON THE SCREEN"
130 FOR L = 1 TO 500:NEXT
140 PRINT "USING A SIMPLE LOOP"
150 FOR L = 1 TO 500:NEXT
160 PRINT "AS A TIME DELAY."
170 FOR L = 1 TO 500:NEXT
Here is the same program using GOSUB:
100 PRINT "THIS PROGRAM PRINTS"
110 GOSUB 200
120 PRINT "SLOWLY ON THE SCREEN"
130 GOSUB 200
140 PRINT "USING A SIMPLE LOOP"
150 GOSUB 200
160 PRINT "AS A TIME DELAY."
170 GOSUB 200
180 END
200 FOR L = 1 TO 500 NEXT
210 RETURN
Each time the program executes a GOSUB
, 5 bytes (including a GOSUB
token, the line number, and the position in the program line are saved in a special area called the "stack," which takes up :warning: FIGURE THIS OUT :warning: bytes of your memory. This limits the amount of data that can be stored in the stack. Therefore, the number of subroutine return addresses that can be stored is limited, and care should be taken to make sure every GOSUB
hits the corresponding RETURN
, or else you'll run out of memory even though you have plenty of bytes free.
RETURN
TYPE: BASIC Flow Control Statement
FORMAT: RETURN
Action:
The RETURN statement is used to exit from a subroutine called for by a GOSUB statement. RETURN restarts the rest of your program at the next executable statement following the GOSUB. If you are nesting subroutines, each GOSUB must be paired with at least one RETURN statement. A subroutine can contain any number of RETURN statements, but the first one encountered will exit the subroutine.
EXAMPLES of RETURN Statement:
10 PRINT"THIS IS THE PROGRAM"
20 GOSUB 1000
30 PRINT"PROGRAM CONTINUES"
40 GOSUB 1000
50 PRINT"MORE PROGRAM"
60 END
1000 PRINT"THIS IS THE GOSUB":RETURN
GOTO
TYPE: BASIC Flow Control Statement
FORMAT: GOTO line_number
FORMAT: GOTO label
Action:
This statement allows the BASIC program to execute lines out of numerical order. The word GOTO
followed by a number will make the
program jump to the line with that number. :warning: GOTO
NOT followed by a number equals GOTO 0. It must have the line number after the word GOTO
. :warning:
It is possible to create loops with GOTO
that will never end. The simplest example of this is a line that GOes TO itself, like 10 GOTO 10
. These loops can be stopped using the RUN/STOP
key or CTRL-C
on the keyboard.
EXAMPLES of GOTO Statement:
GOTO 100
10 GO TO 50
20 GOTO 999
IF ... THEN ...
TYPE: BASIC Flow Control Statement
FORMAT: IF numeric THEN line number or label FORMAT: IF numeric GOTO line number or label FORMAT: IF numeric THEN statements
Action:
This is the statement that gives BASIC most of its "intelligence," the ability to evaluate conditions and take different actions de- pending on the outcome.
The word IF is followed by an expression, which can include numeric literals, variables and functions, comparisons, and arithmetic and logical operators. If the expression evaluates to 0, it is considered false. If it evaluates to any non-zero value it is considered true.
The word THEN appears on the same line and is followed by either a line number or one or more BASIC statements. When the expression is false, everything after the word THEN on that line is ignored, and execution continues with the next line number in the program. A true result makes the program either branch to the line number after the word THEN or execute whatever other BASIC statements are found on that line.
EXAMPLE of IF...GOTO...Statement:
100 INPUT "TYPE A NUMBER"; N
110 IF N <= 0 GOTO 200
120 PRINT "SQUARE ROOT=" SQR(N)
130 GOTO 100
200 PRINT "NUMBER MUST BE >0"
210 GOTO 100
This program prints out the square root of any positive number. The IF statement here is used to validate the result of the INPUT. When the result of N <= 0 is true, the program skips to line 200, and when the result is false the next line to be executed is 120. Note that THEN GOTO is not needed with IF...THEN, as in line 110 where GOTO 200 actually means THEN GOTO 200.
EXAMPLE OF IF...THEN...Statement:
100 FOR L = 1 TO 100
110 IF RND(1) < .5 THEN X=X+1: GOTO 130
120 Y=Y+1
130 NEXT L
140 PRINT "HEADS=" X
150 PRINT "TAILS= " Y
The IF in line 110 tests a random number to see if it is less than .5. When the result is true, the whole series of statements following the word THEN are executed: first X is incremented by 1, then the program skips to line 130. When the result is false, the program drops to the next statement, line 120.
NEXT
TYPE: BASIC Flow Control Statement
FORMAT: NEXT [counter][,counter]...
Action:
The NEXT statement is used with FOR to establish the end of a FOR...NEXT loop. The NEXT need not be physically the last statement in the loop, but it is always the last statement executed in a loop. The counter is the loop index's variable name used with FOR to start the loop. A single NEXT can stop several nested loops when it is followed by each FOR's counter> variable name(s). To do this each name must appear in the order of inner-most nested loop first, to outer-most nested loop last. When using a single NEXT to increment and stop several variable names, each variable name must be separated by commas. Loops can be nested to 9 levels. If the counter variable(s) are omitted, the counter associated with the FOR of the current level (of the nested loops) is incremented.
When the NEXT is reached, the counter value is incremented by 1 or by an optional STEP value. It is then tested against an end-value to see if it's time to stop the loop. A loop will be stopped when a NEXT is found which has its counter value greater than the end-value.
EXAMPLES of NEXT Statement:
10 FOR J=1 TO 5: FOR K=10 TO 20: FOR N=5 TO -5 STEP - 1
20 NEXT N,K,J (Stopping Nested Loops)
10 FOR L=1 TO 100
20 FOR M=1 TO 10
30 NEXT M
400 NEXT L (Note how the loops do NOT cross each other)
10 FOR A=1 TO 10
20 FOR B=1 TO 20
30 NEXT
40 NEXT (Notice that no variable names are needed)
STOP
TYPE: BASIC Flow Control Statement
FORMAT: STOP
Action:
The STOP
statement is used to halt execution of the current program and return to direct mode. Typing the RUN/STOP
key on the keyboard has the same effect as a STOP
statement. The BASIC error message ?BREAK IN LINE nnnnn is displayed on the screen, followed by READY. The "nnnnn" is the line-number where the STOP
occurs. Any open files remain open and all variables are
reserved and can be examined. The program can be restarted by using CONT
or GOTO
statements.
EXAMPLES of STOP Statement:
10 INPUT#1,AA,BB,CC
20 IF AA=BB AND BB=CC THEN STOP
30 STOP
BREAK IN LINE 20 (If the variable AA is -1 and BB is equal to CC then:)
BREAK IN LINE 30 (For any other data values)