Bird
Raised Fist0
PLC Programmingscripting~20 mins

IF-THEN-ELSE statement in PLC Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
IF-THEN-ELSE Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of a simple IF-THEN-ELSE in PLC
What will be the value of the output variable Q after running this PLC code snippet?

IF I = TRUE THEN
  Q := TRUE;
ELSE
  Q := FALSE;
END_IF;
PLC Programming
I := TRUE;
IF I = TRUE THEN
  Q := TRUE;
ELSE
  Q := FALSE;
END_IF;
AQ = TRUE
BQ = FALSE
CQ is unchanged
DSyntax error
Attempts:
2 left
💡 Hint
Think about what happens when the input I is TRUE in the IF condition.
💻 Command Output
intermediate
2:00remaining
IF-THEN-ELSE with numeric comparison
Given the following PLC code, what is the value of Output after execution?

VAR
  Temp : INT := 25;
  Output : STRING;
END_VAR

IF Temp > 30 THEN
  Output := 'Hot';
ELSE
  Output := 'Cold';
END_IF;
PLC Programming
Temp := 25;
IF Temp > 30 THEN
  Output := 'Hot';
ELSE
  Output := 'Cold';
END_IF;
AOutput = 'Hot'
BOutput = 'Cold'
COutput is empty
DRuntime error
Attempts:
2 left
💡 Hint
Check if 25 is greater than 30.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in IF-THEN-ELSE
Which option contains a syntax error in the IF-THEN-ELSE statement in PLC Structured Text?
A
IF X THEN
  Y := TRUE;
ELSE
  Y := FALSE;
END_IF;
B
IF X = TRUE THEN
  Y := TRUE;
ELSE
  Y := FALSE;
END_IF;
C
IF X = TRUE THEN
  Y := TRUE;
ELSE
  Y := FALSE;
END_IF
D
IF X = TRUE THEN
  Y := TRUE
ELSE
  Y := FALSE;
END_IF;
Attempts:
2 left
💡 Hint
Look for missing semicolons or missing END_IF.
🔧 Debug
advanced
2:00remaining
Why does this IF-THEN-ELSE not set the output?
Given this PLC code snippet, why does Output remain FALSE after execution?

VAR
  Input : BOOL := FALSE;
  Output : BOOL := FALSE;
END_VAR

IF Input THEN
  Output := TRUE;
END_IF;
PLC Programming
Input := FALSE;
Output := FALSE;
IF Input THEN
  Output := TRUE;
END_IF;
ABecause the IF statement is missing an ELSE branch.
BBecause Output is initialized to FALSE, it cannot be changed.
CBecause Input is FALSE, the IF condition is not met, so Output stays FALSE.
DBecause the variable Output is not declared.
Attempts:
2 left
💡 Hint
Think about what happens when the IF condition is FALSE.
🚀 Application
expert
3:00remaining
Determine the final value after nested IF-THEN-ELSE
What is the final value of Result after running this nested IF-THEN-ELSE PLC code?

VAR
  A : INT := 5;
  B : INT := 10;
  Result : STRING;
END_VAR

IF A > B THEN
  Result := 'A is greater';
ELSE
  IF A = B THEN
    Result := 'A equals B';
  ELSE
    Result := 'B is greater';
  END_IF;
END_IF;
PLC Programming
A := 5;
B := 10;
IF A > B THEN
  Result := 'A is greater';
ELSE
  IF A = B THEN
    Result := 'A equals B';
  ELSE
    Result := 'B is greater';
  END_IF;
END_IF;
AResult = 'B is greater'
BResult = 'A equals B'
CResult = 'A is greater'
DResult is empty
Attempts:
2 left
💡 Hint
Compare A and B values carefully through the nested conditions.

Practice

(1/5)
1. What does an IF-THEN-ELSE statement do in PLC programming?
easy
A. It repeats a set of instructions multiple times.
B. It sends data to an external device.
C. It stores data in a table format.
D. It chooses between two actions based on a condition.

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    It chooses between two actions based on a condition. -> Option D
  4. Quick Check:

    IF-THEN-ELSE = Choose action based on condition [OK]
Hint: IF-THEN-ELSE picks one of two paths [OK]
Common Mistakes:
  • Confusing IF-THEN-ELSE with loops
  • Thinking it stores data
  • Assuming it sends data externally
2. Which of the following is the correct syntax to start an IF statement in PLC structured text?
easy
A. IF condition THEN
B. IF condition DO
C. WHEN condition THEN
D. IF condition THEN DO

Solution

  1. Step 1: Recall PLC IF statement syntax

    The correct way to start an IF statement is with 'IF condition THEN'.
  2. 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.
  3. Final Answer:

    IF condition THEN -> Option A
  4. Quick Check:

    IF starts with 'IF condition THEN' [OK]
Hint: IF always pairs with THEN, no DO needed [OK]
Common Mistakes:
  • Adding DO after THEN
  • Using WHEN instead of IF
  • Mixing CASE syntax with IF
3. What will be the output of this PLC code snippet?
IF SensorInput = TRUE THEN
  Motor := ON;
ELSE
  Motor := OFF;
END_IF;

Assuming SensorInput is FALSE initially.
medium
A. Motor will keep previous state
B. Motor will be ON
C. Motor will be OFF
D. Syntax error occurs

Solution

  1. Step 1: Evaluate the condition with SensorInput = FALSE

    The condition 'SensorInput = TRUE' is false, so the ELSE branch runs.
  2. Step 2: Determine the action in ELSE branch

    In ELSE, Motor is set to OFF.
  3. Final Answer:

    Motor will be OFF -> Option C
  4. Quick Check:

    Condition false -> ELSE -> Motor OFF [OK]
Hint: False condition triggers ELSE action [OK]
Common Mistakes:
  • Assuming Motor stays ON by default
  • Confusing TRUE and FALSE conditions
  • Thinking ELSE is optional and skipped
4. Identify the error in this PLC IF-THEN-ELSE code:
IF Temperature > 100 THEN
  Alarm := TRUE
ELSE
  Alarm := FALSE;
END_IF;
medium
A. Missing END_IF statement
B. Missing semicolon after Alarm := TRUE
C. Incorrect comparison operator
D. ELSE should be ELSEIF

Solution

  1. Step 1: Check statement endings

    In PLC structured text, each assignment must end with a semicolon. 'Alarm := TRUE' is missing a semicolon.
  2. Step 2: Verify other parts

    Comparison operator '>' is correct, END_IF is present, and ELSE is correct for two branches.
  3. Final Answer:

    Missing semicolon after Alarm := TRUE -> Option B
  4. Quick Check:

    Statements need semicolons [OK]
Hint: Every assignment ends with a semicolon [OK]
Common Mistakes:
  • Omitting semicolons after assignments
  • Confusing ELSE with ELSEIF
  • Forgetting END_IF
5. You want a PLC program to turn ON a fan only if the temperature is above 30°C and the humidity is below 70%. Which IF-THEN-ELSE statement correctly implements this?
hard
A. IF Temperature > 30 AND Humidity < 70 THEN Fan := ON; ELSE Fan := OFF; END_IF;
B. IF Temperature > 30 OR Humidity < 70 THEN Fan := ON; ELSE Fan := OFF; END_IF;
C. IF Temperature > 30 THEN Fan := ON; ELSE IF Humidity < 70 THEN Fan := ON; ELSE Fan := OFF; END_IF;
D. IF Temperature > 30 THEN Fan := ON; END_IF; IF Humidity < 70 THEN Fan := ON; END_IF;

Solution

  1. Step 1: Understand the condition requirements

    The fan should turn ON only if both conditions are true: temperature above 30 and humidity below 70.
  2. 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.
  3. Final Answer:

    IF Temperature > 30 AND Humidity < 70 THEN Fan := ON; ELSE Fan := OFF; END_IF; -> Option A
  4. Quick Check:

    Both conditions must be true -> use AND [OK]
Hint: Use AND to require both conditions true [OK]
Common Mistakes:
  • Using OR instead of AND
  • Using separate IFs causing wrong ON state
  • Misusing ELSE IF for combined conditions
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