Assignment and arithmetic in PLC Programming - Time & Space Complexity
We want to see how the time it takes to run simple math and assignment commands changes as we do more of them.
How does the number of these operations affect the total time?
Analyze the time complexity of the following code snippet.
VAR
i : INT;
result : INT := 0;
END_VAR
FOR i := 1 TO 100 DO
result := result + i;
END_FOR
This code adds numbers from 1 to 100 and stores the sum in result.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Addition and assignment inside the loop.
- How many times: 100 times, once for each number from 1 to 100.
Each time we increase the count of numbers to add, the total operations grow the same amount.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions and assignments |
| 100 | 100 additions and assignments |
| 1000 | 1000 additions and assignments |
Pattern observation: The work grows directly with the number of items we add.
Time Complexity: O(n)
This means the time to finish grows in a straight line as we add more numbers.
[X] Wrong: "Adding numbers inside a loop is instant no matter how many numbers there are."
[OK] Correct: Each addition takes a small amount of time, so more numbers mean more time overall.
Understanding how simple repeated math grows helps you explain how programs handle bigger tasks step by step.
"What if we replaced the loop with two nested loops doing additions? How would the time complexity change?"
Practice
speed := 100;Solution
Step 1: Understand assignment syntax in PLC
PLC uses ':=' to assign values to variables, not '=' or '=='.Step 2: Identify correct assignment operator
The ':=' operator updates the variable with the new value.Final Answer:
The ':=' symbol -> Option AQuick Check:
Assignment operator = ':=' [OK]
- Using '=' instead of ':='
- Confusing comparison '==' with assignment
- Using arrow '->' mistakenly
- Forgetting semicolon after assignment
count in PLC?Solution
Step 1: Check assignment operator usage
PLC uses ':=' for assignment, so '=' and '+=' are incorrect here.Step 2: Verify arithmetic addition syntax
'count := count + 5;' correctly adds 5 to count and assigns it back.Final Answer:
count := count + 5; -> Option AQuick Check:
Use ':=' for assignment and '+' for addition [OK]
- Using '=' instead of ':='
- Trying '+=' which is not valid in PLC
- Writing ':=+' which is invalid
- Missing semicolon
result after executing the following PLC code?temp := 10; result := temp * 3 + 2;
Solution
Step 1: Calculate multiplication first
temp * 3 = 10 * 3 = 30Step 2: Add 2 to the multiplication result
30 + 2 = 32Final Answer:
32 -> Option DQuick Check:
10 * 3 + 2 = 32 [OK]
- Adding before multiplying
- Using wrong variable value
- Ignoring operator precedence
- Confusing temp and result variables
speed := 50 speed := speed + 10;
Solution
Step 1: Check statement endings
PLC statements must end with a semicolon ';'. The first line misses it.Step 2: Confirm other syntax is correct
Second line uses correct ':=' and addition syntax.Final Answer:
Missing semicolon after first assignment -> Option CQuick Check:
Each statement ends with ';' [OK]
- Forgetting semicolon
- Confusing '=' with ':='
- Assuming addition not allowed
- Ignoring variable declaration rules
s1, s2, and s3 in PLC. Which code correctly assigns the average to avg_speed?s1 := 40; s2 := 50; s3 := 60; avg_speed := (s1 + s2 + s3) / 3;
Solution
Step 1: Check correct assignment operator
Use ':=' to assign the calculated value to avg_speed.Step 2: Verify arithmetic expression correctness
Sum all three readings and divide by 3 inside parentheses for average.Final Answer:
avg_speed := (s1 + s2 + s3) / 3; -> Option BQuick Check:
Use ':=' and parentheses for correct average [OK]
- Using '=' instead of ':='
- Missing parentheses causing wrong division
- Multiplying instead of dividing
- Forgetting semicolon
Ready to prove it?
Pass the test and this pattern moves unknown
