Assignment Statement
Assignment statements substitutes a value or a result from a calculation into the variable or PLC data register. The PLC data register where the constant, the value of a variable or the result of a calculation, has to be placed on the left side of the := operator. All assignment statements must end with ;.
- Format: Identifier := Expression
- Identifier can be a variable or PLC data register.
- In Expression, a constant, PLC data register, and the expressions listed in the Operators table can be used.
| Example | Result |
|---|---|
| MX00 := 1; | The value of M0.0 is 1 |
| DW100 := 100; | The value of D100 is 100 |
| DW500 := DW00 + DW01; | The value of D500 is D0 + D1 |
Branch Statement
IF Statement
IF statements perform the operations in the statement if the condition expression result is true.
If the IF statement’s condition is false and an ELSEIF statement exists, the condition of the ELSEIF statement is checked.
If all conditions are false, none of the command statements are performed unless there is ELSE statement exists.
| Format |
|---|
| IF conditional expression THEN logic ELSEIF conditional expression THEN logic ELSE logic END_IF; |
- conditional expressions can be composed of comparison operators or logical operators.
- There can be multiple logic statements.
- Only 1 IF and ELSE statement each can exist. Multiple ELSEIF statements can exist.
| Example |
|---|
| // If D1’s value is 0, assign 1 to D100 IF DW01 = 0 THEN DW100 := 1; // If D1’s value is 1, assign 2 to D100 ELSEIF DW01 = 1 THEN DW100 := 2; // If D1’s value is less than 5, assign 3 to D100 ELSE IF DW01 < 5 THEN DW100 := 3; // If D1’s value is not 0 or 1 and equal to or greater than 5, assign 0 to D100 ELSE DW100 := 0; END_IF; |
CASE Statement
CASE statements branch the following operations according the expression’s value.
| Format |
|---|
| CASE expression OF integer option : logic integer option : logic ELSE logic END_CASE; |
- expression must be an integer
- integer option can be a singular integer, or multiple integers, or a range of integers.
| Integer Option types | Result |
|---|---|
| 1 | When the expression‘s value is 1 |
| 2, 3, 4 | When the expression‘s value is 2, 3, or 4 |
| 5.. 10 | When the expression‘s value is 5, 6, 7, 8, 9, or 10 |
| 1, 2..5, 10 | When the expression‘s value is 1, 2, 3, 4, 5, or 10 |
- There can be multiple logic statements.
- ELSE can only be used once or not all.
| Example |
|---|
| CASE DW10 OF //If D10’s value is 4, assign 4 to D0 4: DW00 := 4; // If D10’s value is between 5 and 6 or 7, assign 5 to D0 5..6, 7: DW00 := 5; // If D10’s value is 4 or between 5 and 6 or not 7, assign 0 to D0 ELSE DW := 0; END_CASE; |
Repetitive Statement
FOR Statement
FOR statements are 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 many repetition statements are used, the scan time will increase and performance will decrease.
| Format |
|---|
| FOR init expression TO expression BY expression DO logic END_FOR; |
- init expression is used to initialize the repetitive variables with an assignment statement. On the left side of the := operator, the variable or PLC data register should exist. On the right side of the := operator, the integer to initialize the variable or PLC data register should exist.
- The expression that follows TO is the final number of the repetitive variable. The integer value must be used, and the logic statements will be performed only when the repetitive variable’s value is less than or equal to this expression.
- The expression that follows BY is the number that increases every time a loop of logic statements is performed. This can be omitted or an integer can be located. The value increases by 1 as default.
- There can be multiple logic statements.
| Example |
|---|
| // This example adds all the values from 1 to 10 // Repetitive variable D0’s increases 1 every loop // The statement loops 10 times // Adds the values from 1 to 10 and stores them in D100 // After the FOR statement ends, D100’s value is 55 FOR DW00 := 1 TO 10 BY 1 DO DW100 := DW100 + DW00; END_FOR; |
WHILE Statement
WHILE statements perform a repetitive operation. WHILE statements are not limited to the number of repetitions like FOR statements.
The logic statements in a WHILE statement will be performed while the conditional expression‘s result is true. It will be performed forever if the conditional expression‘s result never becomes false.
If too many repetition statements are used, the scan time will increase and performance will decrease.
| Format |
|---|
| WHILE conditional expression DO logic END_WHILE; |
- conditional expressions can be composed of comparison operators or logical operators.
- There can be multiple logic statements.
| Example |
|---|
| // This example adds all the values from 0 to 5 // The WHILE statement is operated while D0’s value is less than 5 // D10’s value will be 15 after the WHILE statement terminates WHILE DW00 < 5 DO DW00 := DW00 + 1; DW10 := DW10 + DW00; END_WHILE; The WHILE statement’s conditional expression is always true. This runs forever. In this case, the PLC might disconnect, error LEDs blinking, etc. Refer here to resolve the issue. WHILE DW00 >= 0 DO DW00 := DW00 + 1; DW10 := DW10 + DW00; END_WHILE; |
REPEAT Statement
REPEAT statements perform a repetitive operation. REPEAT statements are not limited to the number of repetitions like FOR statements. REPEAT statements loop performs while the conditional expression after UNTIL is false. The loop will perform forever if the conditional expression doesn’t become true.
Unlike the WHILE statement, the REPEAT statement operates the logic statements once, even if the conditional expression is true.
If too many repetition statements are used, the scan time will increase and performance will decrease.
| Format |
|---|
| REPEAT logic … UNTIL conditional expression END_REPEAT; |
- logic statements are performed while conditional expression is false.
- The REPEAT statements terminates when conditional expression is true.
- There can be multiple logic statements.
| Example |
|---|
| // This example adds all the values from 0 to 5 // If D0’s value is not equal to 5, REPEAT statement is performed // D10’s value will be 15 when REPEAT statement terminates REPEAT DW00 := DW00 + 1; DW10 := DW10 + DW00; UNTIL DW00 = 5 END_REPEAT; The conditional expression after UNTIL is always false. The REPEAT statement runs forever. In this case, the PLC might disconnect, error LEDs blinking, etc. REPEAT DW00 := DW00 + 1; DW10 := DW10 + DW00; UNTIL DW00 < 0 END_REPEAT; |
Other Statements
EXIT Statement
The EXIT statement is used to escape from repetitive statements before they end. When an EXIT statement is performed, the nearest repetitive statement is cancelled.
| Example |
|---|
| // WHILE statement is performed while D0 is less than 10 // If D0 is greater than 5, the logic statement in the IF statement is performed // This will assign 10 to D0 and exit the WHILE statement WHILE DW00 < 10 DO IF DW00 > 5 THEN DW00 := 10; EXIT; END_IF; DW10 := DW10 + DW00; DW00 := DW00 + 1; END_WHILE; |
RETURN Statement
The RETURN statement is used to terminate the current process in the Structured Text program. When the RETURN statement is performed, the logic statements that follows the RETURN statement will be ignored, and the process will jump from the RETURN statement to the last part of the Structured Text program.
| Example |
|---|
| // If D0’s value is greater than 5, assign 10 to D0 and end // The IF statement below satisfies the condition that D0 = D10 // It isn’t performed because of the RETURN IF DW00 > 5 THEN DW00 := 10; RETURN; END_IF; IF DW00 = 10 THEN DW10 := 100; END_IF; |
Comment
Comments make the Structured Text program readable and easy to understand. The comments are not compiled, and they do not affect the program.
// comments the current line (* *) comments the current line and all text in between
| Example |
|---|
| // The logic below is commented: // MX01 := 1; |
| // The logic below is commented: (* MX01 := 1; MX02 := 2; MX03 := 3; MX04 := 4; *) |
