Introduction
Loops help repeat tasks automatically without writing the same steps many times.
FOR loop: FOR variable := start TO end DO // actions END_FOR WHILE loop: WHILE condition DO // actions END_WHILE
FOR i := 1 TO 5 DO // Turn on light END_FOR
WHILE sensorValue < 100 DO
// Increase motor speed
END_WHILEPROGRAM LoopExample VAR i : INT := 0; count : INT := 0; END_VAR // FOR loop example FOR i := 1 TO 3 DO count := count + 1; END_FOR // WHILE loop example WHILE count < 6 DO count := count + 1; END_WHILE // Output the final count // (In real PLC, this might be sent to an HMI or debug console) END_PROGRAM
FOR loop in PLC programming?FOR loop in PLC structured text?FOR i := 1 TO 5 DO ... END_FOR; for loops.VAR i: INT := 1; sum: INT := 0; END_VAR FOR i := 1 TO 3 DO sum := sum + i; END_FOR; // Output sum
VAR i: INT := 0; count: INT := 0; END_VAR WHILE i < 5 DO count := count + 1; END_WHILE;
i < 5, but i never changes inside the loop.i, the condition stays true forever, causing an infinite loop.Q1 to Q4 in sequence using PLC structured text. Which code snippet correctly uses a FOR loop to do this?Q[i] accesses coil Q1 to Q4 correctly if Q is an array of outputs.Q + i. FOR i := 1 TO 4 DO
Qi := TRUE;
END_FOR; treats Q1, Q2 as fixed names, not variable. FOR i := 1 TO 4 DO
Q[i-1] := TRUE;
END_FOR; uses i-1 which may cause off-by-one error if indexing starts at 1.Pass the test and this pattern moves unknown