HTBasic Help
×
Menu
Index

Program Flow Control

 

Unconditional Branching

GOTO line performs unconditional branching to a statement other than the next statement, but its use is discouraged in favor of structured flow control statements.
 
GOTO Fatal_error
 

Subroutines

GOSUB line executes a subroutine that is terminated by RETURN.
 
GOSUB Get_x
. . .
 Get_x: X = Y*45/Z
RETURN
 
This subroutine can be called from many places in the program to save having to duplicate the subroutine statements many times.
 

Conditional Statements

IF...THEN is the most common conditional execution statement. The single line form conditionally branches or executes a single program statement:
 
IF A$="Q" THEN Quit          ! conditional transfer
IF A$="N" THEN PRINT "No !"  ! single statement
 
IF...THEN blocks that end with END IF can contain multiple statements (with an optional ELSE block) that can be nested:
 
IF Xlimit>Upper THEN
  PRINT "The current setting is ";Xlimit
  READ Xlimit
  PRINT "The new setting is ";Xlimit
ELSE
  Xlimit = Xlimit+1
END IF
 

Multi-way Branching

ON...GOTO/GOSUB line performs multi-way branch based on the value of line, which must be from 1 to the number of line references.
 
ON J GOTO First,Secong,Third,Fourth  ! J must be 1, 2, 3, or 4
ON X GOSUB Start,Compute,Done
 

SELECT, CASE, END SELECT

SELECT begins a block which "selects" an expression to be comparison tested CASE by CASE until true or an optional CASE ELSE is reached which will cause that block to be executed. Only the first matching CASE statement will be executed, so order is important. Execution then continues at the line following END SELECT. SELECT blocks can be nested.
 
Each CASE statement specifies a list of numeric or string expressions (matching the SELECT expression type) each separated by a comma. Each expression may specify either a match value, a relational operator (<, <=, =, >=, >, or <>) followed by a match value, or a range specified by a lower and an upper match value.
 
INPUT "What is your age? ",Age
SELECT Age
CASE <1,>100
  PRINT "Invalid response!"
  GOTO Done
CASE <12
  Price = 2
CASE 12 TO 59
  Price = 6
CASE ELSE
  PRINT "Special Rate Tonight:"
  Price = 4.5
END SELECT
PRINT USING """Movie price is $"",D.2D ";Price
 Done: !
END
 

Loops

There are four types of program loops: FOR/NEXT, LOOP/END LOOP, REPEAT/UNTIL, and WHILE/END WHILE.
 
FOR/NEXT loops through a block of code a fixed number of times or until prematurely exited with GOTO. You may optionally specify a positive or negative STEP value (the default is 1). The termination value is tested before the loop is executed the first time and execution continues at the line following NEXT when true:
 
J=50 TO 100 STEP 2
  READ A(J)
NEXT J
 
WHILE/END WHILE, LOOP/END LOOP, and REPEAT/UNTIL test for loop termination at the beginning, the end, or inside the loop. Loops can be nested.
 
WHILE X>4 ! test at start of loop
   ! do this...
END WHILE
 
LOOP
   ! do this...
EXIT IF  ! test at middle of loop
   ! do this...
END LOOP
 
REPEAT
   ! do this...
UNTIL X=4  ! test at end of loop