Using the THEN/ELSE and ON ERROR Clauses
A substantial number of DataBasic statements use the THEN/ELSE clause to enable conditional execution of a sequence of statements, or one of two sequences of statements. Other statements use the ON ERROR clause only to enable execution of a sequence of statements if the primary statement fails. The ON ERROR clause is functionally equivalent to the ELSE clause and its use is always optional.
Syntax of THEN/ELSE
The syntax for using THEN/ELSE clauses is illustrated below by the IF statement.
Single line THEN/ELSE
IF expression [THEN statement{;statement}...| ELSE statement{;statement}...]
Single line THEN/Multi-line ELSE
IF expression THEN statement{;statement}...ELSE
statement
.
.
END
Multi-line THEN/Single line ELSE
IF expression THEN
statement
.
.
END {ELSE statement{;statement}...}
Multi-line THEN/Multi-line ELSE
IF expression THEN
statement
.
.
END {ELSE
statement
.
.
END}
Multi-line ELSE only
IF expression ELSE
statement
.
.
END
Syntax of ON ERROR Clause
The syntax for using the ON ERROR clause is illustrated below by the WRITE statement.
Single line ON ERROR
WRITE string ON ERROR statement{;statement}...
Multi-line ON ERROR
WRITE string ON ERROR
statement
.
.
END
Comments
Any number of valid DataBasic statements can be executed following a THEN, ELSE or ON ERROR clause. Multiple statements can either be included on the same line as the reserved word THEN, ELSE or ON ERROR, in which case they should be separated by semi-colons, or they can be on separate lines following the THEN, ELSE or ON ERROR, in which case the conditional sequence must be terminated with an END statement.
Statements using THEN/ELSE
The following DataBasic statements require a THEN or ELSE clause, except as noted by asterisks:
INPUT * |
INPUT@ * |
||
LOCK ** |
|||
|
|
* A THEN or ELSE clause is only required in an INPUT or INPUT@ statement as part of a FOR clause and not by the statement alone.
** Both the THEN and ELSE clauses are optional with the LOCK statement.
Statements using ON ERROR
The ON ERROR clause is optionally used with the following statements to specify an alternative action if the statement fails.
|
|
Examples
IF X=0 ELSE STOP
If the value of X is zero, control passes to the next statement; otherwise, the program terminates.
IF X=0 THEN Y=1;Z=2;ELSE Y=2;Z=2
If X equals 0, the values 1 and 2 are assigned to variables Y and Z respectively; otherwise, both Y and Z are given the value 2.
OPEN "STOCK" TO STOCK ELSE PRINT "Error Opening Stock File" GOSUB 9000 END DELETE STOCK,C159 ON ERROR PRINT "Item C159 cannot be deleted" GOTO 300 END
Opens the STOCK data section and assigns it to the file variable STOCK. If it cannot open it, the program displays the error message 'Error opening Stock File' and branches to Subroutine 9000. If it is opened successfully, the program tries to delete item C159 in the STOCK file. Failure to delete the item invokes the ON ERROR clause which outputs the error message 'Item C159 cannot be deleted' and branches to Statement label 300.
SELECT STOCK TO STOCK.LIST INPUT ITEM.ID READ STOCK.ITEM FROM STOCK.LIST, ITEM.ID THEN STOCK.NO.=STOCK.NO.+1 PRINT "Number of ":STOCK.ITEM<1>": in stock is ":STOCK.ITEM<4> END ELSE PRINT "Error reading from item ":ITEM.NO
Selects the STOCK file variable and generates STOCK.LIST. It then requests an item-id and on receiving the item-id attempts to read the associated item from the STOCK.LIST into a dynamic array. If it is successful, it increments STOCK.NO and outputs a message containing the type of item and the number currently in stock. If it fails to read the item, it outputs an error message. See the description of Dynamic arrays.
INPUT OVERTIME.HOURS, 2 WITH "/" FOR 50 THEN GOSUB 3000 RETURN END ELSE PRINT "TOO LATE!" GOSUB 9000 RETURN END
Waits 5 seconds for the user to enter up to 2 characters into OVERTIME. HOURS, terminating input with a '/ ' character. A correct input branches the program to subroutine 3000. Failure to enter the characters properly leads to an error message and branches the program to subroutine 9000.