Bird
Raised Fist0
PLC Programmingscripting~30 mins

FOR and WHILE loops in PLC Programming - Mini Project: Build & Apply

Choose your learning style10 modes available
Using FOR and WHILE Loops in PLC Programming
📖 Scenario: You are programming a simple PLC (Programmable Logic Controller) to control a conveyor belt system in a factory. The system needs to count items passing on the belt and stop the belt after a certain number of items have passed.
🎯 Goal: Build a PLC program that uses a FOR loop to simulate counting items and a WHILE loop to keep the conveyor running until the count reaches a limit.
📋 What You'll Learn
Create a variable itemCount to track the number of items counted.
Create a variable maxItems to set the maximum number of items before stopping.
Use a FOR loop to simulate counting items from 1 to maxItems.
Use a WHILE loop to simulate the conveyor running until itemCount reaches maxItems.
Print the current itemCount during each loop iteration.
Print a message when the conveyor stops.
💡 Why This Matters
🌍 Real World
Counting items on a conveyor belt and controlling machinery based on counts is common in manufacturing automation.
💼 Career
Understanding loops and control flow in PLC programming is essential for automation engineers and technicians working with industrial control systems.
Progress0 / 4 steps
1
Create the initial variables
Create a variable called itemCount and set it to 0. Create another variable called maxItems and set it to 5.
PLC Programming
Hint

Use the assignment operator := to set values in PLC code.

2
Set up the FOR loop to count items
Write a FOR loop that uses a variable i to count from 1 to maxItems. Inside the loop, set itemCount to i.
PLC Programming
Hint

Use FOR i := 1 TO maxItems DO to start the loop and END_FOR; to end it.

3
Use a WHILE loop to run the conveyor
Reset itemCount to 0. Write a WHILE loop that runs while itemCount is less than maxItems. Inside the loop, increase itemCount by 1 and print the current itemCount.
PLC Programming
Hint

Reset with itemCount := 0; Use WHILE condition DO and END_WHILE; to create the loop. Use Print to show messages.

4
Print the conveyor stop message
After the WHILE loop, write a Print statement to display the message 'Conveyor stopped after ' followed by the itemCount and the text ' items.'.
PLC Programming
Hint

Use Print with string concatenation and INT_TO_STRING to show the final message.

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