If-then-else – Rockwell Automation 1771-DB BASIC MODULE User Manual

Page 167

Advertising
background image

Chapter
Statements

11

11 -15

Use the IF-THEN-ELSE statement to set up a conditional test.

If you want to transfer control to different line numbers using the IF
statement, you may omit the GOTO statement. These examples give the
same results:

>20 IF INT(A)<10 THEN GOTO 100 ELSE GOTO 200

or

>20 IF INT(A)<10 THEN 100 ELSE 200

You can replace the THEN statement with any valid BASIC module
statement:

>30 IF A<>10 THEN PRINT A ELSE 10

>30 IF A<>10 PRINT A ELSE 10

You may execute multiple statements following the THEN or ELSE if you
use a colon to separate them:

>30 IF A<>10 THEN PRINT A : GOTO 150 ELSE 10

You may omit the ELSE statement. If you omit the ELSE statement
control passes to the next statement.

>1 REM EXAMPLE PROGRAM

>20 IF A=10 THEN 40

>30 PRINT A

Syntax

IF

rel expr THEN valid statement ELSE valid statement

Example

>1 REM EXAMPLE PROGRAM

>10 IF A =100 THEN A=0 ELSE A=A+1

Upon execution of line 10 IF A is equal to 100, THEN A is assigned a
value of 0. IF A does not equal 100, A is assigned a value of A+1.

IF-THEN-ELSE

Advertising