Basic programming guide, If then else – Remote Processing BASIC for the CX-10 Modbus User Manual

Page 34

Advertising
background image

BASIC PROGRAMMING GUIDE

2-25

IF THEN ELSE

Syntax:

IF expr [ THEN ] statement(s) [ ELSE statement(s)]
Where: expr = any logical evaluation or variable

statement(s) = any number of Basic statements

Function:

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

Mode:

Run

Use:

10 IF A<>B THEN PRINT "A=B" ELSE PRINT "A<>B"

DESCRIPTION

THEN is implied by IF. You may omit THEN. ELSE is optional. It is included when an "either - or"
situation is encountered.

In the case of multiple statements per line following an IF-THEN-ELSE, Basic executes the following
statements only if expr was true. This enables you to conditionally execute multiple statements with a single
expr test. Remember this applies only to Basic statements separated by the {:} delimiter and on the same
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 above program takes about 1 second to execute, which translates to about 1 ms/ line for this example. 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

Advertising