Repetitive Statement

FOR Statement

FOR Statement is performed to process the repetitive operations.
This statement can only be used when the number of loops is definitive. If not, WHILE or REPEAT statements should be used.

If too much of repetitive statements are used, it will increase the scan time dramatically which might interfere with the operation of the other programs or system.


Format

FOR Init_Expression TO Expression BY Expression DO

   Syntaxes

END_FOR;

Init_Expression is to initialize the repetitive variables. Assignment statement is used. On the left side of the operator ‘:=’, the variable or PLC Device should be located. On the right side of the operator, the integer to initialize the variable/PLC Device should be located.
The Expression that follows TO is the final number of the repetitive variable. The integer value must be used and the syntaxes will be performed only when the repetitive variable’s value is smaller than or equal to this Expression.
The Expression that follows BY is the number that increases every time a loop of syntaxes is performed. This can be omitted or an integer can be located. The value increases by 1 as default.
There can be multiple Syntaxes..


Example

Description

// This example adds all the values from 1 to 10

// Repetitive variable D00’s value will increase by 1 every time a loop is performed. The statement will be looped 10 times.

// Adds the values from 1 to 10 and stores in D100

// After FOR Statement ends, D100’s value will be 55.

FOR DW00 := 1 TO 10 BY 1 DO

   DW100 := DW100 + DW00;

END_FOR;

WHILE Statement

WHILE Statement does not have a limited number of repetitions like FOR statement does.
The syntaxes in WHILE statement will be performed while the Conditional Expression’s result is "1".
It will be performed forever if the Conditional Expression’s value never becomes "0".

If too much of repetitive statements are used, it will increase the scan time dramatically which might interfere with the


Format

WHILE Conditional_Expression DO

   Syntaxes

END_WHILE;

Conditional Expressions can be composed of Comparison Operation or Logical Operation
There can be multiple Syntaxes.


Example

Description

// This example adds all the values from 0 to 5.

// WHILE Statement is operated while D0’s value is less than 5

// D10’s value will be 15 after WHILE Statement is finished

WHILE DW00 < 5 DO

   DW00 := DW00 + 1;

   DW10 := DW10 + DW00;

END_WHILE;

// WHILE Statement’s Conditional Expression is always "1". Therefore, this repetitive statement will be performed forever.

// In this case, there might be PLC malfunctions such as disconnection from CICON, error LEDs blinking in special modules, etc.

// If the malfunction happens, please check [Here] to solve the issue.

WHILE DW00 >= 0 DO

   DW00 := DW00 + 1;

   DW10 := DW10 + DW00;

END_WHILE;

REPEAT Statement

REPEAT Statement does not have a limited number of repetitions like FOR statement does.
The loop is performed while the Conditional Expression after UNTIL is "0".
The loop will be performed forever if the Conditional Expression doesn’t become "1".
Unlike WHILE Statement, REPEAT Statement operates the syntaxes for once even if the Conditional Expression’s value is "1".

If too much of repetitive statements are used, it will increase the scan time dramatically which might interfere with the


Format

REPEAT

   Syntaxes

   …

UNTIL Conditional Expression

END_REPEAT;

Syntaxes are performed while Conditional Expression is false.
REPEAT Statement ends when the Conditional Expression becomes true.
There can be multiple Syntaxes.
Conditional Expressions can be composed of Comparison Operation or Logical Operation

Example

Description

// This example adds all the values from 0 to 5.

// If D0’s value is not equal to 5, REPEAT statement will be performed.

// D10’s value will be 15 when REPEAT Statement is finished.

REPEAT

   DW00 := DW00 + 1;

   DW10 := DW10 + DW00;

UNTIL DW00 = 5

END_REPEAT;

// The Conditional Expression after UNTIL is always false.

// Therefore, the repetitive statement will be performed forever.

// In this case, there might be PLC malfunctions such as disconnection from CICON, error LEDs blinking in special modules, etc.

// If the malfunction happens, please check [Here] to solve the issue.

 

REPEAT

   DW00 := DW00 + 1;

   DW10 := DW10 + DW00;

UNTIL DW00 < 0

END_REPEAT;