6 Programming
6-98
NJ-series CPU Unit Software User’s Manual (W501)
Precautions:
• WHILE must always be used together with END_WHILE.
• If the <condition_expression> is FALSE before <statement> is executed, the WHILE construct is
exited and <statement> is not executed.
• You can write <statement_1> and <statement_2> on multiple lines. Separate statements with a
semicolon (;).
•
You can execute more than one statement for <statement>. Separate statements with a semicolon (;).
• You can also specify a BOOL variable (including functions that return a BOOL value) for the condi-
tion expressions instead of an actual expression.
Example:
Example 1: The first multiple of 7 that exceeds 1,000 is calculated and assigned to variable A.
Example 2: The value of variable X is doubled if X is less than 3,000 and the value is assigned
to array variable element DATA[1]. Next, the value of X is doubled again and the
value is assigned to the array variable element DATA[2]. This process is repeated.
• If you do not write correct condition expressions, the program execution time increases and may
cause a Task Period Exceeded Error.
Example:
boolVar := TRUE;
WHILE boolVar DO
intVar := intVar + INT#1;
END_WHILE;
REPEAT
The following expressions are used to specify whether the condition is met.
TRUE: The condition is met.
FALSE: The condition is not met.
Overview:
This construct repeatedly executes one or more statements until a condition expression is TRUE.
Reserved Words:
REPEAT, UNTIL, END_ REPEAT
Construct Structure:
A := 0;
WHILE A <= 1000 DO
A := A+INT#7;
END_WHILE;
n := 1;
X := 1;
WHILE X < 3000 DO
X:= X*INT#10#2;
DATA[n]:= X;
n := n+INT#1;
END_WHILE;
REPEAT
<statement>;
UNTIL <condition_expression>
END_REPEAT;