Comparison instructions (EQ, NE, GT, LT) in PLC Programming - Time & Space Complexity
We want to understand how the time to run comparison instructions changes as we use them more in a program.
Specifically, how does running EQ, NE, GT, or LT instructions affect execution time when repeated?
Analyze the time complexity of the following code snippet.
// Example: Check array values against a threshold
FOR i := 0 TO n - 1 DO
IF array[i] > threshold THEN
output[i] := TRUE;
ELSE
output[i] := FALSE;
END_IF;
END_FOR;
This code compares each element in an array to a threshold using the GT (greater than) instruction.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The GT comparison inside the loop.
- How many times: Exactly once for each element in the array, so n times.
Each new element adds one more comparison operation.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 comparisons |
| 100 | 100 comparisons |
| 1000 | 1000 comparisons |
Pattern observation: The number of comparisons grows directly with the number of elements.
Time Complexity: O(n)
This means the time to run these comparison instructions grows in a straight line as the input size grows.
[X] Wrong: "Comparison instructions take constant time no matter how many times they run, so the whole loop is constant time."
[OK] Correct: Each comparison runs once per element, so more elements mean more comparisons and more time.
Understanding how simple comparisons add up helps you explain how programs handle data step-by-step, a key skill in automation and control programming.
"What if we added a nested loop that compares every element to every other element? How would the time complexity change?"
Practice
Solution
Step 1: Understand the meaning of EQ
EQ stands for 'equal' and checks if two values are exactly the same.Step 2: Compare with other instructions
NE means not equal, GT means greater than, and LT means less than, so they do not check equality.Final Answer:
EQ -> Option AQuick Check:
EQ means equal [OK]
- Confusing EQ with NE
- Thinking GT checks equality
- Mixing LT with EQ
Solution
Step 1: Identify the comparison for 'greater than'
GT means 'greater than', so to check if A is greater than B, use 'A GT B'.Step 2: Check other options
LT means less than, EQ means equal, NE means not equal, so they are incorrect for this check.Final Answer:
IF A GT B THEN -> Option DQuick Check:
GT means greater than [OK]
- Using LT instead of GT
- Confusing EQ with GT
- Using NE for greater than check
VAR X: INT := 5; Y: INT := 10; Result: BOOL; END_VAR Result := X LT Y;
What will be the value of
Result after execution?Solution
Step 1: Understand the comparison LT
LT means 'less than'. The code checks if X (5) is less than Y (10).Step 2: Evaluate the condition
Since 5 is less than 10, the condition is true, so Result will be TRUE.Final Answer:
TRUE -> Option CQuick Check:
5 LT 10 = TRUE [OK]
- Thinking LT means greater than
- Assuming Result is FALSE
- Confusing syntax with error
IF A NEQ B THEN // do something END_IF
Solution
Step 1: Check the comparison instruction used
The code uses 'NEQ' which is not a standard PLC comparison instruction.Step 2: Identify correct instruction
The correct instruction for 'not equal' is 'NE', not 'NEQ'.Final Answer:
NEQ is not a valid comparison instruction -> Option BQuick Check:
Use NE for not equal, not NEQ [OK]
- Using NEQ instead of NE
- Forgetting THEN keyword
- Adding unnecessary parentheses
Solution
Step 1: Translate the condition into comparisons
The motor should turn on if temperature is greater than 50 (GT 50) and pressure is less than 100 (LT 100).Step 2: Check each option
IF Temperature GT 50 AND Pressure LT 100 THEN Motor := TRUE; uses GT and LT with AND, matching the condition exactly. Other options use wrong comparisons or OR instead of AND.Final Answer:
IF Temperature GT 50 AND Pressure LT 100 THEN Motor := TRUE; -> Option AQuick Check:
GT and LT with AND matches condition [OK]
- Using OR instead of AND
- Using EQ instead of GT or LT
- Mixing NE with equality checks
Ready to prove it?
Pass the test and this pattern moves unknown
