BASIC Flow Control functions

From DenialWIKI
Jump to navigation Jump to search

IF THEN

This evaluates a statement to see if it is true and if so executes the code after the then. If the statement is not true then it continues to the next line

Example:

IF 1=1 THEN PRINT "TRUE"
TRUE

READY.
IF 1=2 THEN PRINT "FALSE"

READY.

RUN

This starts executing the BASIC program in memory. If a program calls RUN from inside it will restart the program and program state will be lost (the variables will be reset)

10 PRINT "HELLO"
RUN
HELLO

READY.
10 PRINT "HELLO"
20 RUN
RUN
HELLO
HELLO
HELLO
...forever or until you break the program.

END

Ends the running program.

10 GET A$
20 IF A$ = "Q" THEN END
30 GOTO 10
RUN

READY.

STOP

Functions like END but prints the line number the program exited on

10 GET A$
20 IF A$ = "Q" THEN STOP
30 GOTO 10
RUN

BREAK IN 20
READY.


FOR NEXT STEP

FOR variable = start TO end STEP step If STEP is missing then step defaults to 1

When the FOR statement is first called the variable is set to the start value. Then BASIC executes code until the NEXT statement is reached, it then adds the step value (normally 1) to the variable and checks if the variable is going to equal or more (or less if step is negative) than the end value.

10 FOR I = 1 TO 4
20 PRINT I
30 NEXT
RUN
1
2
3
4

READY.
10 FOR I = 1 TO 4 STEP 2
20 PRINT I
30 NEXT
RUN
1
3

READY.
10 FOR I = 1 TO 4 STEP 3
20 PRINT I
30 NEXT
RUN
1
4

READY.
10 FOR I = 1 TO 4 STEP 5
20 PRINT I
30 NEXT
RUN
1

READY.
10 FOR I = 4 TO 1 STEP -1
20 PRINT I
30 NEXT
RUN
4
3
2
1

READY.
10 FOR I = 4 TO 1 STEP -5
20 PRINT I
30 NEXT
RUN
4

READY.

It is also possible to have nested loops. These are FOR loops inside of each other. The internal FOR loop is executed inside the external loop.

10 FOR X = 1 TO 5
20 PRINT X;
30 FOR I = 1 TO 5
40 PRINT I;
50 NEXT I
60 PRINT
70 NEXT X
RUN
 1  1  2  3  4  5
 2  1  2  3  4  5
 3  1  2  3  4  5
 4  1  2  3  4  5
 5  1  2  3  4  5

READY.

It is also possible to combine NEXT statements together

10 FOR X = 1 TO 5
20 FOR I = 1 TO 5
30 PRINT I;
40 NEXT I,X

Is the same as

10 FOR X = 1 TO 5
20 FOR I = 1 TO 5
30 PRINT I;
40 NEXT I
50 NEXT X

Optionally you can leave the variable off the NEXT statement to save memory. This does not work if you combine NEXT statments

10 FOR I = 1 TO 5
20 NEXT

Is the same as

10 FOR I = 1 TO 5
20 NEXT I

GOTO

Start execution at the specified line.

10 PRINT "THIS IS COOL"
20 GOTO 10
RUN
THIS IS COOL
THIS IS COOL
THIS IS COOL
...forever or until you break the program.

GOSUB RETURN

Start execution at specified line and then return to where it was last called.

For example

10 GOSUB 100
20 PRINT "WORLD"
30 END
100 PRINT "HELLO"
110 RETURN
RUN

HELLO
WORLD
READY.

Note that GOSUB uses a stack to record where to return to. It has a depth of 23???

10 I = I + 1
20 PRINT I;
30 GOSUB 10
RUN
 1  2  3  4  5  6  7
8  9  10  11  12  13
14  15  16  17  18  19
  20  21  22  23
?OUT OF MEMORY
 ERROR IN 10
READY.

Most programs will never need that much as they only GOSUB 2-3 deep. But it is important to note that if you are doing recursive subroutines you will have to use GOTO and create your own return system.

If you call RETURN without having called GOSUB first you will encounter the following error:

?RETURN WITHOUT GOSUB
 ERROR IN line#

Where line# is the line number that the return was called from.

CONT

Resumes program execution after RUN/STOP has been pressed or the END or STOP command has been called.

10 FOR I = 1 TO 5
20 PRINT I
30 NEXT I
RUN
1
2
3

BREAK IN 20
READY.
CONT
4
5

Note you can alter program variables and then resume the program. Also you can examine variable contents and then resume the program.

10 FOR I = 1 TO 2000
20 PRINT I
30 END
40 NEXT I
RUN
1

READY.
CONT
2

READY.

Note if RUN/STOP and RESTORE is pressed then the program state is lost and CONT won't work.

READY.
CONT

?CAN'T CONTINUE
 ERROR
READY.

LIST

LIST will display the contents of the BASIC program stored in memory. It can be given parameters so that the entire program is not listed.

Examples:

10 A=1
20 B=1
30 C=1
40 D=1
LIST

10 A=1
20 B=1
30 C=1
40 D=1
READY.

LIST -20 

10 A=1
20 B=1

READY.
LIST 20-

20 B=1
30 C=1
40 D=1

READY.

LIST 20
20 B=1

LIST 20-30

20 B=1
30 C=1

READY.

LIST 30-20

READY.

NEW

NEW will delete any BASIC program in memory. It is not often used in a program.

Example:

10 PRINT "HELLO"
LIST

10 PRINT "HELLO"
READY.
NEW

READY.
LIST

READY.