11 - 86 11 - 86
MELSEC-Q
11 INSTRUCTIONS AND FUNCTIONS
IF THEN ELSE
Instruction
IF to THEN toELSE
• Selects an instruction to execute according to the result of the expression.
IF <conditional expression> THEN <instruction> / <line number> [: <instruction> / <line
number> ] [ ELSE <instruction> / <line number>]
conditional expression
• • • •
Specify by a relational operation expression ( <, >,
=).
Syntax
instruction or line number
• • • •
Specify an instruction to execute or a line number to
branch after THEN or ELSE.
IF X=Y THEN PRINT “Hit!”
ELSE PRINT “Miss!”
• • • •
Displays “Hit!” if X and Y are the same, or displays
“Miss!” otherwise.
Examples
IF X=0 THEN 100 ELSE
200
• • • •
Executes line 100 if X is 0 or executes line 200
otherwise.
Description
• The IF to THEN to ELSE instruction selects an instruction to execute or a line number to
branch according to on the result of the expression.
• If the condition of <conditional expression> is true, the instructions after THEN are
executed. Instructions after ELSE are ignored.
• If the condition of <conditional expression> is false, the instructions after THEN are ignored
and instructions after ELSE are executed.
The execution moves to the instruction of the next line if ELSE is not specified.
• <line number> can also be specified by a label.
• The IF instruction can be nested by writing another IF instruction after THEN or ELSE.
Example IF <conditional expression 1)> THEN IF <conditional expression 2)> THEN
Instruction 1) ELSE Instruction 2) ELSE ELSE Instruction 3 )
.
Instruction
3)
Conditional
expression
1)
Conditional
expression
2)
YES
YES
Instruction
1)
Instruction
2)
NO
NO