Flow Control Statements

FOR – TO – NEXT

FOR variable_name = initial_value TO end_value [ STEP step_value ] NEXT variable_name

FOR is the first statement of a FOR loop.

NEXT is the last statement of a FOR loop.

The loop variable varies from the initial_value to the end_value by the step_value increment (or decrement)
in each execution of the body of the loop (statements between the FOR and NEXT statements).
If the loop variable exceeds the value of the end_value, the program executes the statement following the NEXT statement.

If the STEP keyword and the step_value are missing, the step is assumed to be 1.

Note

Changing the step_value during the execution of the loop has no effect.

A global variable is not allowed as a loop control variable.

Example 1:

FOR i=1 TO 10 STEP 2
    PRINT i
NEXT i

Example 2:

! The two program fragments below are equivalent:

! 1st
a = b
1:
IF c > 0 AND a > d OR c < 0 AND a < d THEN 2
PRINT a
a = a + c
GOTO 1

! 2nd
2:
FOR a = b TO d STEP c
    PRINT a
NEXT a

The above example shows that step_value = 0 causes an infinite loop.


Only one NEXT statement is allowed after a FOR statement.
You can exit the loop with the GOTO command and to return after leaving,
but you cannot enter a loop skipping the FOR statement.

DO – WHILE

DO [statment1
    statement2
    ...
    statementn]
WHILE condition

The statements between the keywords are executed as long as the condition is true.

The condition is checked after each execution of the statements.

WHILE – ENDWHILE

WHILE condition DO
    [statement1
    statement2
    ...
    statementn]
ENDWHILE

The statements between the keywords are executed as long as the condition is true.

The condition is checked before each execution of the statements.

REPEAT – UNTIL

REPEAT [statement1
    statement2
    ...
    statementn]
UNTIL condition

The statements between the keywords are executed until the condition becomes true.

The condition is checked after each execution of the statements.

Example:
The following four sequences of GDL commands are equivalent

! 1st
FOR i = 1 TO 5 STEP 1
    BRICK 0.5, 0.5, 0.1
    ADDZ 0.3
NEXT i

! 2nd
i = 1
DO
    BRICK 0.5, 0.5, 0.1
    ADDZ 0.3
    i = i + 1
WHILE i <= 5

! 3rd
i = 1
WHILE i <= 5 DO
    BRICK 0.5, 0.5, 0.1
    ADDZ 0.3
    i = i + 1
ENDWHILE

! 4th
i = 1
REPEAT
    BRICK 0.5, 0.5, 0.1
    ADDZ 0.3
    i = i + 1
UNTIL i > 5

 

IF – GOTO

IF condition THEN label
IF condition GOTO label
IF condition GOSUB label

Conditional jump statement. If the value of the condition expression is 0 (logical ‘false’), the command has no effect,
otherwise execution continues at the label. THEN, GOTO or THEN GOTO are equivalent in this context.

 

Example:

IF a THEN 28
IF i > j GOTO 200+i*j
IF i > 0 GOSUB 9000

 

IF – THEN – ELSE – ENDIF

IF condition THEN statement [ELSE statement]
IF condition THEN
    [statement1
    statement2
    ...
    statementn]
[ELSE
    statementn+1
    statementn+2
    ...
    statementn+m]
ENDIF

If you write only one command after keywords THEN and/or ELSE in the same row, there is no need for ENDIF.
A command after THEN or ELSE in the same row means a definite ENDIF.

If there is a new row after THEN, the successive commands
(all of them until the keyword ELSE or ENDIF) will only be executed if the expression in the condition is true (other than zero).
Otherwise, the commands following ELSE will be carried out. If the ELSE keyword is absent, the commands after ENDIF will be carried out.

 

Example:

IF a = b THEN height = 5 ELSE height = 7
IF needDoors THEN
    CALL "door_macro" PARAMETERS
    ADDX a
ENDIF
IF simple THEN
    HOTSPOT2 0, 0
    RECT2 a, 0, 0, b
ELSE PROJECT2 3, 270, 1
IF name = "Sphere" THEN
    ADDY b
    SPHERE 1
ELSE
    ROTX 90
    TEXT 0.002, 0, name
ENDIF

 

GOTO

GOTO label

Unconditional jump statement. The program executes a branch to the statement denoted by the value of the label (numerical or string).
Variable label expressions can slow down interpretation due to runtime jumping address determination.

 

Example:

GOTO K+2

 

GOSUB

GOSUB label

Internal subroutine call where the label is the entry point of the subroutine. Label value can be any numerical or string expression.
Variable label expressions can slow down interpretation due to runtime jumping address determination.

RETURN

RETURN

Return from an internal subroutine.

END / EXIT

END [v1, v2, ..., vn]
EXIT [v1, v2, ..., vn]

End of the current GDL script. The program terminates or returns to the level above.
It is possible to use several ENDs or EXITs in a GDL file. If the optional list of values is specified,
the current script will pass these return values to its caller.

Note: the number of possible returned elements is limited at 32767 items.


See the description of receiving returned parameters at the CALL command.

BREAKPOINT

BREAKPOINT expression

With this command, you can specify a breakpoint in the GDL script.
The GDL debugger will stop at this command if the value of the parameter (a numeric expression) is true (1)
and the Enable Breakpoints option of the debugger is checked.
In normal execution mode, the GDL interpreter simply steps over this command.