beautypg.com

Rpbasic-52 programming guide, If then else – Remote Processing BASIC 52 User Manual

Page 66

background image

RPBASIC-52 PROGRAMMING GUIDE

2-47

IF THEN ELSE

Syntax:

IF expr [ THEN ] statement(s) [ ELSE statement(s)]
Where: expr = any logica l evaluation or varia ble

statement(s) = any num ber of Basic sta tements

Function:

When expr is TRUE (not zero), the instruction following THEN is executed, otherwise the
instruction following ELSE is executed.

Mode:

Run

Use:

1 0 IF A < > B T H E N PR I N T " A =B " E L S E PR I N T " A <> B "

Cards:

All

D E S C R IP T I ON

THE N is implied by IF. You m ay omit T HEN . ELSE is o ptional. It is included w hen an "eithe r - or"
situation is encountered.

In the case of multiple statements per line following an IF-THEN-EL SE, Basic executes the following
statemen ts only if expr was true. This e nables you to c onditionally exe cute multiple statements w ith a single
expr t es t. R em e m b er th is a pp li e s o n ly to B a si c s ta t em e n ts se p ar a te d by th e {: } de l im i te r a n d o n th e sa m e
program line.

expr can be either a logical evaluation (=, <, >, <>, .AND., .OR., .XOR., or .NOT.) or a variable. Using a
simple variable as a flag can speed up program execution. The following examples illustrate different
execution speeds.

10 A = 1000
20 CLEAR TICK(0)
30 IF A<>0 THEN A=A-1 : GOTO 30
40 PRINT TICK(0)

The abov e program takes about 1 se cond to exec ute, which transla tes to about 1 m s/ line for this exam ple. If
line 30 were re-written as:

30 IF A THEN A=A-1 : GOTO 30

Execution time is reduced by about 20% by taking away the "<>0" evaluation.

RELATED none

ERRORS

none

EXAMPLE

10 A = 1
20 IF A=0 THEN PRINT "A is 0" ELSE PRINT "A is non-zero"

>run

Is non-zero