ST statements
Statements run from top to bottom each time the routine is called. End assignments, calls and completed control blocks with a semicolon.
Assignment and comments
:= stores the value on the right in the tag on the left. = tests equality. A // comment ends at the line break. A (* … *) comment may span lines.
Count — DINT; MotorRun — BOOL.// Add one part
Count := Count + 1;
(* Keep the output off
until the next decision. *)
MotorRun := FALSE;IF / ELSIF / ELSE
The first true condition wins. ELSE runs when none is true; ELSIF and ELSE are optional. A tag you do not assign keeps its previous value.
StopPB — BOOL; StartPB — BOOL; MotorRun — BOOL.IF StopPB THEN
MotorRun := FALSE;
ELSIF StartPB THEN
MotorRun := TRUE;
ELSE
MotorRun := FALSE;
END_IF;CASE
The selector is DINT. Labels are whole numbers; separate alternatives with commas or use an inclusive range such as 2..4. ELSE handles unmatched values.
Count — DINT; MotorRun — BOOL.CASE Count OF
0, 1: MotorRun := FALSE;
2..4: MotorRun := TRUE;
ELSE
MotorRun := FALSE;
END_CASE;FOR and BY
Use a DINT counter tag. The end value is inclusive. BY sets a nonzero DINT step; omit it for 1. A negative step counts down. This example adds 1, 3 and 5 during one call.
Count — DINT; Index — DINT.Count := 0;
FOR Index := 1 TO 5 BY 2 DO
Count := Count + Index;
END_FOR;WHILE
Checks before each iteration, so its body may run zero times. Change something that can make the condition false.
Index — DINT.Index := 0;
WHILE Index < 3 DO
Index := Index + 1;
END_WHILE;REPEAT / UNTIL
Checks after the body, so it runs at least once. It stops when UNTIL becomes true.
Index — DINT.Index := 0;
REPEAT
Index := Index + 1;
UNTIL Index >= 3
END_REPEAT;EXIT and RETURN
EXIT leaves the innermost loop and continues after it. RETURN leaves the routine immediately and returns to its caller; in the Main Routine it ends that routine’s work for this scan.
StopPB — BOOL; Index — DINT; Count — DINT.IF StopPB THEN
RETURN;
END_IF;
FOR Index := 1 TO 5 DO
IF Index = 3 THEN
EXIT;
END_IF;
Count := Index;
END_FOR;Loops run within one scan, without waiting for the next input update. Use a tag that advances over successive scans for a sequence that must wait.