Series logic (AND condition) in PLC Programming - Time & Space Complexity
We want to understand how the time to check a series of conditions connected by AND grows as the number of conditions increases.
How does the number of checks change when we add more conditions in a series?
Analyze the time complexity of the following code snippet.
// Example of series logic with AND condition
BOOL result := TRUE;
FOR i := 0 TO n-1 DO
result := result AND inputs[i];
END_FOR;
// Final result is TRUE only if all inputs are TRUE
This code checks a list of boolean inputs one by one, combining them with AND logic to get a final TRUE or FALSE.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each input value and combining it with the current result using AND.
- How many times: Exactly once for each input, so n times if there are n inputs.
As the number of inputs grows, the number of checks grows in the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of operations grows directly with the number of inputs.
Time Complexity: O(n)
This means the time to complete the checks grows in a straight line as you add more inputs.
[X] Wrong: "Since the AND stops if one input is FALSE, the time is always constant."
[OK] Correct: While short-circuiting can happen, in worst case all inputs must be checked, so time grows with input size.
Understanding how series logic scales helps you explain how your code behaves with many inputs, a useful skill in automation and control tasks.
"What if we changed the AND condition to OR? How would the time complexity change?"
Practice
Solution
Step 1: Understand AND condition meaning
AND means all inputs must be true together to get true output.Step 2: Apply to series logic
In series logic, inputs are connected so output is true only if every input is true.Final Answer:
All input conditions must be true for the output to be true -> Option DQuick Check:
AND means all true = output true [OK]
- Thinking only one input needs to be true
- Confusing AND with OR logic
- Assuming output toggles without input conditions
Solution
Step 1: Identify series AND syntax
Series AND means contacts connected one after another in a line before output coil.Step 2: Match with options
--[ ]--[ ]--( )--where both contacts are in series shows two contacts in series before output coil, which is correct.Final Answer:
--[ ]--[ ]--( )-- where both contacts are in series -> Option CQuick Check:
Series contacts before coil = AND logic [OK]
- Choosing parallel contacts which mean OR logic
- Placing output coil before inputs
- Missing output coil in the rung
--[ I1 ]--[ I2 ]--( Q1 )--What will be the state of Q1 if I1 = TRUE and I2 = FALSE?
Solution
Step 1: Analyze input states in series
Since I1 is TRUE but I2 is FALSE, the series AND condition fails because both must be TRUE.Step 2: Determine output Q1 state
Output Q1 will be FALSE because the AND condition requires all inputs TRUE.Final Answer:
Q1 will be FALSE -> Option AQuick Check:
AND needs all TRUE, here I2 is FALSE, so output FALSE [OK]
- Assuming output is TRUE if one input is TRUE
- Thinking output toggles without input change
- Ignoring the FALSE input in series
--[ I1 ]--+--[ I2 ]--( Q1 )--
|
( )What is wrong with this design?
Solution
Step 1: Examine input connection style
The '+' symbol and branching show I1 and I2 are connected in parallel, not series.Step 2: Understand effect on logic
Parallel inputs mean OR logic, not AND series logic as intended.Final Answer:
Inputs I1 and I2 are in parallel, not series -> Option AQuick Check:
Parallel = OR, not series AND [OK]
- Confusing parallel with series wiring
- Ignoring ladder diagram symbols
- Assuming output coil is missing
Solution
Step 1: Identify series AND condition
All switches must be connected in series (one after another) before output coil.Step 2: Check each option
--[ S1 ]--[ S2 ]--[ S3 ]--( M1 )--shows S1, S2, S3 in series before M1 coil, correct for AND logic.Final Answer:
--[ S1 ]--[ S2 ]--[ S3 ]--( M1 )-- -> Option BQuick Check:
Series contacts = AND condition for motor start [OK]
- Using parallel branches which create OR logic
- Placing output coil before inputs
- Mixing series and parallel incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
