3 - 25 3 - 25
MELSEC-Q
3 LET'S CREATE AND EXECUTE A PROGRAM
3.6.4 Loop while a certain condition is met
Use the WHILE-WEND instructions to repeat execution of instructions only while a
certain condition is met.
10 N=0
20 WHILE N<4
30 PRINT N;
40 N=N+1
50 WEND
60 END
RUN
0 1 2 3
OK
Instructions in this range are repeated.
This is a condition that is true as long as N is less than 4.
The instructions in the loop are repeated while the condition stated immediately after
WHILE is met.
For details on how to specify conditional expressions, see Section 3.7.
The WHILE-WEND instructions are loop instructions very similar to the FOR-NEXT
instructions, but it is the WHILE instruction that determines whether or not the loop
should continue. Therefore, if a condition is not met from the beginning, the instructions
between the WHILE and the WEND will not be executed even once.
10 FOR N=1 TO 1
20 PRINT N
30 NEXT N
40 END
RUN
1
OK
10 N=1
20 WHILE N<1
30 PRINT N
40 WEND
50 END
RUN
1
OK
FOR-NEXT loop
WHILE-WEND loop
The NEXT instruction determines
whether the loop should continue.