If-then-else, Purpose, Syntax – Rockwell Automation 1746-BAS BASIC LANGUAGE User Manual

Page 92: Examples

Advertising
background image

Publication 1746-RM001A-US-P

7-8 Control Functions

If line 100 exists, this statement causes execution of the program to resume at line
100. If line number 100 does not exist, the message

ERROR: INVALID LINE

NUMBER

is printed to the console device and the module enters the Command

mode.

Unlike the RUN command, the GOTO statement, if executed in the Command
mode, does not clear the variable storage space or interrupts. However, if the
GOTO statement is executed in the Command mode after a line is edited, the
module clears the variable storage space and all BASIC evoked interrupts.

IF-THEN-ELSE

Purpose

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

Syntax

IF [rel expr] THEN valid statement ELSE valid statement

Examples

Example 1

>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 you want to transfer
control to different line numbers using the IF statement, you may omit the GOTO
statement. The following 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 module statement as shown
below:

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

>30 IF A<>10 PRINT A ELSE 10

Advertising