53
IF-THEN-ELSE / IF-GOTO-ELSE
PURPOSE
: Executes the THEN statement or GOTO statement when the specified
condition is met. The ELSE statement is executed when the specified condition is not
met.
FORMAT
:
THEN
statement
statement
IF
condition
[ : statement
ELSE
[ : statement
Numeric expression
GOTO
branch destination
branch destination
branch destination line number
Line number
With branch destination:
# program area number
] ]*
Single character; 0-9
*Label
Label name
EXAMPLE
: IF A=0 THEN 300 ELSE 400
IF K$=”Y” THEN PRINT X ELSE PRINT Y
PARAMETERS
:
1. Branch condition: Numeric expression truncated to an integer
2. Branch destination line number: integer in the range of 1
≤
line number
≤
65535
3. Program area number: single character, 0-9
4. Label: Name of a label in the program.
EXPLANATION
:
1. The statement following the THEN clause is executed, or execution jumps to
the destination specified by the GOTO statement when the branch condition is
met.
2. If the branch condition is not met, the statement following the ELSE statement
is executed, or the program jumps to the specified branch destination.
Execution proceeds to the next program line when the ELSE statement is
omitted.
3. The format “IF A THEN –“ results in the condition being met when value of the
expression (A) is not 0 (absolute value of A > 10
-99
). The condition is not met
when the value of the expression is 0.
4. IF statements can be nested (an IF statement may contain other IF
statements). In this case, the THEN – ELSE statements are related by their
proximity. The GOTO – ELSE combinations have the same relationships.
IF – THEN IF THEN – ELSE IF – THEN ELSE – ELSE –
SAMPLE PROGRAM
:
10 INPUT “1 TO 9”;A
20 IF (0<A)AND(A<10) THEN PRINT “GOOD!” ELSE 10
“GOOD!” is displayed for input values from 1 to 9. Re-input is requested for other
values.
P
0