Bird
Raised Fist0
PLC Programmingscripting~10 mins

FOR and WHILE loops in PLC Programming - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - FOR and WHILE loops
Start
↓
Initialize Loop Variable
↓
Check Loop Condition
↓
Execute Loop Body
↓
Update Loop Variable
↩Back to Check Loop Condition
This flow shows how FOR and WHILE loops start by setting a variable, check a condition, run the loop body if true, update the variable, and repeat until the condition is false.
Execution Sample
PLC Programming
FOR i := 1 TO 3 DO
  output := output + i;
END_FOR;

WHILE output < 10 DO
  output := output + 2;
END_WHILE;
This code adds numbers 1 to 3 to output using a FOR loop, then adds 2 repeatedly using a WHILE loop until output is at least 10.
Execution Table
StepLoop TypeVariable iConditionActionOutput Value
1FOR11 <= 3 (True)output = 0 + 11
2FOR22 <= 3 (True)output = 1 + 23
3FOR33 <= 3 (True)output = 3 + 36
4FOR44 <= 3 (False)Exit FOR loop6
5WHILE-6 < 10 (True)output = 6 + 28
6WHILE-8 < 10 (True)output = 8 + 210
7WHILE-10 < 10 (False)Exit WHILE loop10
💡 FOR loop ends when i=4 fails condition i <= 3; WHILE loop ends when output reaches 10 and condition output < 10 is false.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6After Step 7
i-2344444
output0136681010
Key Moments - 3 Insights
Why does the FOR loop stop after i becomes 4?
Because the condition i <= 3 becomes false at i=4, so the loop exits as shown in execution_table row 4.
Why does the WHILE loop check the condition before adding 2 to output?
WHILE loops check the condition first; if false, the loop body does not run. This is shown in execution_table rows 5-7.
What happens to the variable i during the WHILE loop?
Variable i does not change during the WHILE loop; it stays at 4 as shown in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of output after the FOR loop finishes?
A6
B10
C3
D0
💡 Hint
Check the Output Value column at Step 4 where the FOR loop exits.
At which step does the WHILE loop condition become false?
AStep 5
BStep 6
CStep 7
DStep 4
💡 Hint
Look at the Condition column for the WHILE loop in execution_table rows 5-7.
If the FOR loop started at i := 2 TO 4, what would output be after the FOR loop?
A6
B9
C10
D12
💡 Hint
Sum 2 + 3 + 4 = 9; check how output accumulates in the FOR loop steps.
Concept Snapshot
FOR loop: repeats with a counter from start to end.
WHILE loop: repeats while condition is true.
FOR initializes and updates counter automatically.
WHILE checks condition before each iteration.
Loops stop when condition is false.
Full Transcript
This lesson shows how FOR and WHILE loops work in PLC programming. The FOR loop runs a set number of times, increasing a variable each time. The WHILE loop runs as long as a condition is true. We traced a program adding numbers with FOR, then adding 2 repeatedly with WHILE until a limit. Variables i and output change step by step. The FOR loop stops when i exceeds 3. The WHILE loop stops when output reaches 10. Understanding when loops start, run, and stop helps write clear automation scripts.

Practice

(1/5)
1. Which statement best describes a FOR loop in PLC programming?
easy
A. It repeats a set of instructions a fixed number of times.
B. It repeats instructions only while a condition is true.
C. It runs instructions once and then stops.
D. It waits for an input before running.

Solution

  1. Step 1: Understand the purpose of a FOR loop

    A FOR loop repeats instructions a specific number of times, controlled by a counter.
  2. Step 2: Compare with other loop types

    WHILE loops repeat based on a condition, not a fixed count. The other options describe different behaviors.
  3. Final Answer:

    It repeats a set of instructions a fixed number of times. -> Option A
  4. Quick Check:

    FOR loop = fixed repetitions [OK]
Hint: FOR loops run fixed times; WHILE loops depend on conditions [OK]
Common Mistakes:
  • Confusing FOR loops with WHILE loops
  • Thinking FOR loops run only once
  • Believing FOR loops wait for input
2. Which of the following is the correct syntax for a FOR loop in PLC structured text?
easy
A. for i in range(1,6): ...
B. FOR (i = 1; i <= 5; i++) { ... }
C. FOR i := 1 TO 5 DO ... END_FOR;
D. WHILE i < 5 DO ... END_WHILE;

Solution

  1. Step 1: Identify PLC structured text syntax

    PLC uses FOR i := 1 TO 5 DO ... END_FOR; for loops.
  2. Step 2: Recognize other languages' syntax

    The syntax FOR (i = 1; i <= 5; i++) { ... } is C/JavaScript. The syntax for i in range(1,6): ... is Python. WHILE i < 5 DO ... END_WHILE; is a WHILE loop.
  3. Final Answer:

    FOR i := 1 TO 5 DO ... END_FOR; -> Option C
  4. Quick Check:

    PLC FOR loop uses := and TO [OK]
Hint: PLC FOR loops use ':=' and 'TO' keywords [OK]
Common Mistakes:
  • Using C or Python syntax in PLC code
  • Confusing FOR and WHILE syntax
  • Omitting END_FOR statement
3. What will be the output of this PLC structured text code?
VAR i: INT := 1; sum: INT := 0; END_VAR
FOR i := 1 TO 3 DO
  sum := sum + i;
END_FOR;
// Output sum
medium
A. Syntax error
B. 6
C. 0
D. 3

Solution

  1. Step 1: Trace the FOR loop iterations

    The loop runs with i = 1, 2, 3. sum starts at 0.
  2. Step 2: Calculate sum after each iteration

    sum = 0+1=1, then 1+2=3, then 3+3=6.
  3. Final Answer:

    6 -> Option B
  4. Quick Check:

    1+2+3 = 6 [OK]
Hint: Add loop variable each iteration to sum [OK]
Common Mistakes:
  • Adding only last value of i
  • Starting sum at 1 instead of 0
  • Confusing loop range boundaries
4. Identify the error in this PLC code snippet:
VAR i: INT := 0; count: INT := 0; END_VAR
WHILE i < 5 DO
  count := count + 1;
END_WHILE;
medium
A. Missing increment of i inside the WHILE loop
B. Incorrect variable declaration syntax
C. WHILE loop should be FOR loop
D. count variable should be BOOL, not INT

Solution

  1. Step 1: Analyze the WHILE loop condition and body

    The loop condition is i < 5, but i never changes inside the loop.
  2. Step 2: Identify infinite loop risk

    Without incrementing i, the condition stays true forever, causing an infinite loop.
  3. Final Answer:

    Missing increment of i inside the WHILE loop -> Option A
  4. Quick Check:

    Loop variable must change to avoid infinite loop [OK]
Hint: Always update loop variable inside WHILE loops [OK]
Common Mistakes:
  • Forgetting to increment loop variable
  • Confusing FOR and WHILE loops
  • Incorrect variable types
5. You want to use a loop to turn on 4 output coils named Q1 to Q4 in sequence using PLC structured text. Which code snippet correctly uses a FOR loop to do this?
hard
A. FOR i := 1 TO 4 DO Q[i-1] := TRUE; END_FOR;
B. FOR i := 1 TO 4 DO Q + i := TRUE; END_FOR;
C. FOR i := 1 TO 4 DO Qi := TRUE; END_FOR;
D. FOR i := 1 TO 4 DO Q[i] := TRUE; END_FOR;

Solution

  1. Step 1: Understand coil addressing in arrays

    Using Q[i] accesses coil Q1 to Q4 correctly if Q is an array of outputs.
  2. Step 2: Check other options for syntax errors

    FOR i := 1 TO 4 DO Q + i := TRUE; END_FOR; uses invalid syntax 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.
  3. Final Answer:

    FOR i := 1 TO 4 DO Q[i] := TRUE; END_FOR; -> Option D
  4. Quick Check:

    Use array indexing for sequential coil control [OK]
Hint: Use array indexing Q[i] for sequential outputs [OK]
Common Mistakes:
  • Using invalid syntax for coil names
  • Off-by-one errors in indexing
  • Treating coil names as strings
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard