IF-THEN-ELSE statement in PLC Programming - Time & Space Complexity
We want to understand how the time it takes to run an IF-THEN-ELSE statement changes as the input changes.
Specifically, we ask: does the decision-making take more time when inputs grow?
Analyze the time complexity of the following code snippet.
IF sensor_value > threshold THEN
motor_speed := high_speed;
ELSE
motor_speed := low_speed;
END_IF;
This code checks a sensor value and sets the motor speed based on that check.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Single IF condition check
- How many times: Exactly once per execution
The IF-THEN-ELSE runs one check regardless of input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 check |
| 100 | 1 check |
| 1000 | 1 check |
Pattern observation: The number of operations stays the same no matter how big the input is.
Time Complexity: O(1)
This means the time to run the IF-THEN-ELSE does not grow with input size; it stays constant.
[X] Wrong: "The IF-THEN-ELSE takes longer if the input value is bigger or more complex."
[OK] Correct: The IF-THEN-ELSE only checks one condition once, so input size or value does not affect how many steps it takes.
Understanding that simple decisions like IF-THEN-ELSE run in constant time helps you explain how your code behaves efficiently in real tasks.
"What if we added a loop around the IF-THEN-ELSE to check multiple sensor values? How would the time complexity change?"
Practice
IF-THEN-ELSE statement do in PLC programming?Solution
Step 1: Understand the purpose of IF-THEN-ELSE
The IF-THEN-ELSE statement checks a condition and decides which action to take based on that condition.Step 2: Compare with other options
Repeating instructions is done by loops, storing data is done by variables or tables, and sending data is communication, not decision making.Final Answer:
It chooses between two actions based on a condition. -> Option DQuick Check:
IF-THEN-ELSE = Choose action based on condition [OK]
- Confusing IF-THEN-ELSE with loops
- Thinking it stores data
- Assuming it sends data externally
Solution
Step 1: Recall PLC IF statement syntax
The correct way to start an IF statement is with 'IF condition THEN'.Step 2: Check other options for errors
'DO' is not used after IF in PLC structured text, 'WHEN' is for CASE statements, and 'THEN DO' is invalid syntax.Final Answer:
IF condition THEN -> Option AQuick Check:
IF starts with 'IF condition THEN' [OK]
- Adding DO after THEN
- Using WHEN instead of IF
- Mixing CASE syntax with IF
IF SensorInput = TRUE THEN Motor := ON; ELSE Motor := OFF; END_IF;
Assuming
SensorInput is FALSE initially.Solution
Step 1: Evaluate the condition with SensorInput = FALSE
The condition 'SensorInput = TRUE' is false, so the ELSE branch runs.Step 2: Determine the action in ELSE branch
In ELSE, Motor is set to OFF.Final Answer:
Motor will be OFF -> Option CQuick Check:
Condition false -> ELSE -> Motor OFF [OK]
- Assuming Motor stays ON by default
- Confusing TRUE and FALSE conditions
- Thinking ELSE is optional and skipped
IF Temperature > 100 THEN Alarm := TRUE ELSE Alarm := FALSE; END_IF;
Solution
Step 1: Check statement endings
In PLC structured text, each assignment must end with a semicolon. 'Alarm := TRUE' is missing a semicolon.Step 2: Verify other parts
Comparison operator '>' is correct, END_IF is present, and ELSE is correct for two branches.Final Answer:
Missing semicolon after Alarm := TRUE -> Option BQuick Check:
Statements need semicolons [OK]
- Omitting semicolons after assignments
- Confusing ELSE with ELSEIF
- Forgetting END_IF
Solution
Step 1: Understand the condition requirements
The fan should turn ON only if both conditions are true: temperature above 30 and humidity below 70.Step 2: Check each option's logic
IF Temperature > 30 AND Humidity < 70 THEN Fan := ON; ELSE Fan := OFF; END_IF; uses AND, requiring both conditions. IF Temperature > 30 OR Humidity < 70 THEN Fan := ON; ELSE Fan := OFF; END_IF; uses OR, which turns fan ON if either condition is true. IF Temperature > 30 THEN Fan := ON; ELSE IF Humidity < 70 THEN Fan := ON; ELSE Fan := OFF; END_IF; turns fan ON if either condition is true via ELSE IF. IF Temperature > 30 THEN Fan := ON; END_IF; IF Humidity < 70 THEN Fan := ON; END_IF; turns fan ON separately for each condition, possibly turning ON incorrectly.Final Answer:
IF Temperature > 30 AND Humidity < 70 THEN Fan := ON; ELSE Fan := OFF; END_IF; -> Option AQuick Check:
Both conditions must be true -> use AND [OK]
- Using OR instead of AND
- Using separate IFs causing wrong ON state
- Misusing ELSE IF for combined conditions
Ready to prove it?
Pass the test and this pattern moves unknown
