Skip to content

Flow control

Every program is a story about order. Structured blocks tell that story most clearly; line targets remain available when the original algorithm genuinely thinks in lines.

IF

Block form

IF Temperature# < 0 THEN
  State$ = "ICE"
ELSEIF Temperature# < 100 THEN
  State$ = "WATER"
ELSE
  State$ = "STEAM"
END IF

Use as many ELSEIF arms as needed. ELSE is optional.

Single-line form

IF Lives% = 0 THEN PRINT "GAME OVER" ELSE Lives% = Lives% - 1

Single-line IF is good for a truly small choice. It becomes hard to scan once either arm has more than one idea.

Classic target forms are accepted too:

IF Done% THEN 900
IF Failed% GOTO Recovery

SELECT CASE

SELECT CASE is the readable answer when one value chooses among several shapes:

SELECT CASE Score%
  CASE 100
    Rank$ = "PERFECT"
  CASE 80 TO 99
    Rank$ = "EXCELLENT"
  CASE IS >= 60
    Rank$ = "PASS"
  CASE 0, 1, 2
    Rank$ = "JUST STARTED"
  CASE ELSE
    Rank$ = "TRY AGAIN"
END SELECT

A CASE can contain comma-separated exact values, inclusive value TO value ranges, or IS followed by a relation.

FOR ... NEXT

Use FOR when a counter and a limit describe the repetition:

FOR I% = 1 TO 10
  PRINT I%; I% * I%
NEXT I%

STEP may be positive, negative, or computed:

FOR I% = 10 TO 0 STEP -2
  PRINT I%
NEXT I%

NEXT may repeat the variable name. Doing so is useful in nested loops:

FOR Row% = 0 TO 2
  FOR Column% = 0 TO 2
    PRINT Grid%(Row%, Column%);
  NEXT Column%
  PRINT
NEXT Row%

Leave early with EXIT FOR.

WHILE ... WEND

WHILE tests before the body:

WHILE Remaining% > 0
  Remaining% = Remaining% - 1
WEND

If the condition begins false, the body does not run. EXIT WHILE provides an explicit early exit.

DO ... LOOP

DO supports top-tested and bottom-tested forms, with either WHILE or UNTIL:

DO WHILE Connected%
  CALL PumpOneMessage
LOOP
DO
  Attempts% = Attempts% + 1
LOOP UNTIL Ready% OR Attempts% = 3

A bottom-tested loop executes at least once. Use EXIT DO for an early escape.

Labels, GOTO, and GOSUB

A target is a numbered line or named label:

GOSUB DrawBanner
GOTO Continue

DrawBanner:
  PRINT "D/BASIC"
  RETURN

Continue:
  PRINT "READY"

GOSUB remembers a return point; RETURN resumes there. Prefer a SUB when the routine needs parameters, local variables, a declared contract, or reuse across modules. GOSUB remains excellent for preserving the shape of a small classic listing.

Computed branches

ON expression GOTO and ON expression GOSUB choose a target by one-based position:

ON Choice% GOTO RedRoom, GreenRoom, BlueRoom
ON MenuItem% GOSUB NewFile, OpenFile, SaveFile

This is compact for transferred code. SELECT CASE is usually easier to extend safely in new code.

Procedure exits

The legal structured exits are:

EXIT FOR
EXIT DO
EXIT WHILE
EXIT SUB
EXIT FUNCTION

An exit names the construct it leaves. There is no ambiguous unlabelled break that changes meaning when code is nested.

END and STOP

END completes a console program with status 0:

PRINT "ALL DONE"
END

STOP is an intentional interruption and completes with status 3. It is useful while teaching, probing, or marking a path that should be conspicuous:

IF Impossible% THEN STOP

Neither is a refusal. A refusal is the runtime saying it cannot truthfully perform a requested operation.

Refusal control

ON REFUSAL GOTO target establishes the language's governed refusal handler. RESUME NEXT continues after the refused operation; RESUME target continues at an explicit target.

ON REFUSAL GOTO CouldNotProvide

CALL AskForOptionalCapability
PRINT "AVAILABLE"
GOTO Finished

CouldNotProvide:
  PRINT "UNAVAILABLE:"; ERR; " AT "; ERL
  RESUME Finished

Finished:
END

Use this for a truly optional runtime operation, not to hide programming mistakes or catch a pre-start admission failure. Errors, Refusals & Diagnostics explains catchability, recovery, and cleanup; Compatibility, Migration & Refusals explains the permanent machine-access boundary.


Next: Procedures & Functions →