Bird
Raised Fist0
PLC Programmingscripting~15 mins

IF-THEN-ELSE statement in PLC Programming - Deep Dive

Choose your learning style10 modes available
Overview - IF-THEN-ELSE statement
What is it?
An IF-THEN-ELSE statement is a way to make decisions in a PLC program. It checks if a condition is true or false. If the condition is true, it runs one set of instructions. If false, it runs a different set of instructions or does nothing.
Why it matters
Without IF-THEN-ELSE, a PLC would do the same thing all the time, no matter what. This would make machines less flexible and safe. IF-THEN-ELSE lets the PLC respond to different situations, like stopping a motor if a sensor detects a problem.
Where it fits
Before learning IF-THEN-ELSE, you should understand basic PLC programming and how to read inputs and write outputs. After mastering IF-THEN-ELSE, you can learn about loops, timers, and more complex control structures.
Mental Model
Core Idea
An IF-THEN-ELSE statement chooses between two paths based on whether a condition is true or false.
Think of it like...
It's like a traffic light deciding if cars should stop or go: if the light is green, cars go; else, cars stop.
┌───────────────┐
│ Check Condition│
└───────┬───────┘
        │True
        ▼
   ┌─────────┐
   │ Execute │
   │  THEN   │
   └─────────┘
        │
        ▼
      End
        ▲
        │False
   ┌─────────┐
   │ Execute │
   │  ELSE   │
   └─────────┘
        │
        ▼
      End
Build-Up - 6 Steps
1
FoundationBasic IF condition check
🤔
Concept: Learn how to write a simple IF statement that checks one condition.
In PLC programming, an IF statement looks like this: IF input_signal THEN output_signal := TRUE; END_IF; This means: if the input_signal is ON (true), then turn ON the output_signal.
Result
When input_signal is ON, output_signal turns ON. Otherwise, output_signal stays OFF.
Understanding the simplest IF statement is the first step to controlling machine behavior based on sensor inputs.
2
FoundationAdding ELSE for alternative action
🤔
Concept: Introduce ELSE to handle the case when the IF condition is false.
The IF-THEN-ELSE structure: IF input_signal THEN output_signal := TRUE; ELSE output_signal := FALSE; END_IF; This means: turn ON output_signal if input_signal is ON; otherwise, turn it OFF.
Result
Output_signal always matches the state of input_signal: ON when input_signal is ON, OFF otherwise.
ELSE ensures the program explicitly defines what happens when the condition is false, avoiding unpredictable outputs.
3
IntermediateUsing multiple conditions with AND/OR
🤔Before reading on: do you think IF-THEN-ELSE can check more than one condition at once? Commit to yes or no.
Concept: Learn to combine conditions using logical operators AND and OR inside IF statements.
Example: IF input1 AND input2 THEN output := TRUE; ELSE output := FALSE; END_IF; This means: output turns ON only if both input1 and input2 are ON.
Result
Output is ON only when both inputs are ON; otherwise, output is OFF.
Combining conditions lets the PLC make more precise decisions, reflecting complex real-world requirements.
4
IntermediateNested IF-THEN-ELSE statements
🤔Before reading on: do you think you can put an IF-THEN-ELSE inside another IF-THEN-ELSE? Commit to yes or no.
Concept: Learn how to place IF-THEN-ELSE statements inside others to handle multiple decision layers.
Example: IF input1 THEN IF input2 THEN output := TRUE; ELSE output := FALSE; END_IF; ELSE output := FALSE; END_IF; This means: output is ON only if input1 and input2 are both ON.
Result
Output is ON only when both input1 and input2 are ON; OFF otherwise.
Nested IFs allow step-by-step decision making, useful for complex control logic with multiple checks.
5
AdvancedIF-THEN-ELSE in ladder logic
🤔Before reading on: do you think IF-THEN-ELSE statements look the same in ladder logic as in text-based PLC languages? Commit to yes or no.
Concept: Understand how IF-THEN-ELSE logic translates into ladder diagrams, the graphical PLC programming style.
In ladder logic, IF conditions are represented by contacts (normally open or closed) and outputs by coils. Example: Two contacts in series represent AND condition; parallel contacts represent OR. The ELSE part is handled by using separate rungs or using NOT contacts. This visual style shows the flow of electricity representing logic decisions.
Result
The ladder diagram visually controls outputs based on input conditions, equivalent to IF-THEN-ELSE in text.
Knowing how IF-THEN-ELSE maps to ladder logic helps bridge text and graphical PLC programming, improving troubleshooting.
6
ExpertAvoiding common IF-THEN-ELSE pitfalls
🤔Before reading on: do you think missing ELSE in IF statements can cause unexpected behavior? Commit to yes or no.
Concept: Learn subtle bugs caused by incomplete IF-THEN-ELSE usage and how to write robust conditions.
If ELSE is missing, the output may retain its previous state unexpectedly. Example: IF input THEN output := TRUE; END_IF; If input is false, output stays as it was, which may cause errors. Also, overlapping conditions or improper nesting can cause logic conflicts. Best practice: always define ELSE or default states explicitly.
Result
Programs behave predictably, avoiding stuck outputs or conflicting commands.
Understanding these pitfalls prevents hard-to-find bugs that can cause machine downtime or unsafe states.
Under the Hood
The PLC scans the program cyclically. During each scan, it evaluates the IF condition. If true, it executes the THEN block; if false, it executes the ELSE block if present. This decision changes the state of outputs or variables immediately for that scan cycle.
Why designed this way?
IF-THEN-ELSE was designed to mimic human decision-making in automation. It provides clear, readable logic that matches real-world conditions. Alternatives like jump statements were avoided to keep programs structured and easier to debug.
┌───────────────┐
│ Start Scan    │
└───────┬───────┘
        │
        ▼
┌───────────────┐
│ Evaluate IF   │
│ Condition?    │
└───────┬───────┘
        │Yes / No
   ┌────┴────┐
   │         │
┌──▼──┐  ┌───▼───┐
│ THEN│  │ ELSE  │
│Block│  │ Block │
└──┬──┘  └───┬───┘
   │         │
   └────┬────┘
        ▼
  ┌───────────┐
  │ Update    │
  │ Outputs   │
  └────┬──────┘
       ▼
   ┌─────────┐
   │ End Scan│
   └─────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does an IF statement always need an ELSE part? Commit to yes or no.
Common Belief:IF statements work fine without ELSE; the program will handle false conditions automatically.
Tap to reveal reality
Reality:Without ELSE, the program may leave outputs unchanged when the condition is false, causing unexpected behavior.
Why it matters:This can cause outputs to stay ON or OFF incorrectly, leading to machine errors or safety risks.
Quick: Can you use multiple ELSE parts in one IF-THEN-ELSE statement? Commit to yes or no.
Common Belief:You can have many ELSE parts to handle different cases in one IF statement.
Tap to reveal reality
Reality:Only one ELSE is allowed; to handle multiple cases, you must use ELSE IF or nested IF statements.
Why it matters:Misunderstanding this leads to syntax errors or logic that doesn't run as expected.
Quick: Does the order of conditions in IF-THEN-ELSE affect program behavior? Commit to yes or no.
Common Belief:Order does not matter; all conditions are checked equally.
Tap to reveal reality
Reality:Order matters because conditions are checked top to bottom; earlier conditions can prevent later ones from running.
Why it matters:Incorrect order can cause wrong decisions or missed conditions, leading to faulty control.
Quick: Is IF-THEN-ELSE logic the same in all PLC programming languages? Commit to yes or no.
Common Belief:IF-THEN-ELSE works exactly the same in all PLC languages and editors.
Tap to reveal reality
Reality:Syntax and behavior can vary between languages like Structured Text, Ladder Logic, or Function Block Diagram.
Why it matters:Assuming uniformity can cause errors when switching between PLC platforms or languages.
Expert Zone
1
Short-circuit evaluation in combined conditions means the PLC stops checking as soon as the result is known, improving efficiency.
2
In ladder logic, ELSE logic often requires creative rung design since there is no direct ELSE keyword.
3
Stacking nested IFs too deeply can make programs hard to read and maintain; using CASE or SELECT statements can be better.
When NOT to use
IF-THEN-ELSE is not ideal for very complex decision trees or when many conditions must be checked independently. Alternatives like CASE statements, state machines, or function blocks provide clearer and more maintainable solutions.
Production Patterns
In real factories, IF-THEN-ELSE is used for safety checks, mode switching, and simple interlocks. Experts combine it with timers, counters, and alarms to build robust control systems that respond safely and predictably.
Connections
Boolean Logic
IF-THEN-ELSE uses Boolean logic to decide true or false paths.
Understanding Boolean logic helps grasp how conditions combine and evaluate in IF statements.
Decision Trees (Computer Science)
IF-THEN-ELSE structures form the branches of decision trees.
Seeing IF statements as branches in a decision tree clarifies how complex decisions are broken down step-by-step.
Human Decision Making (Psychology)
IF-THEN-ELSE mimics how humans make choices based on conditions.
Recognizing this connection helps design intuitive control logic that matches human reasoning.
Common Pitfalls
#1Leaving out ELSE causes outputs to hold old values unexpectedly.
Wrong approach:IF sensor = TRUE THEN motor := ON; END_IF;
Correct approach:IF sensor = TRUE THEN motor := ON; ELSE motor := OFF; END_IF;
Root cause:Not realizing that without ELSE, the output does not reset when the condition is false.
#2Using multiple ELSE parts in one IF statement causes syntax errors.
Wrong approach:IF input1 THEN output := 1; ELSE output := 2; ELSE output := 3; END_IF;
Correct approach:IF input1 THEN output := 1; ELSIF input2 THEN output := 2; ELSE output := 3; END_IF;
Root cause:Misunderstanding that only one ELSE is allowed; multiple conditions require ELSE IF or ELSIF.
#3Incorrect nesting leads to logic that never runs or conflicts.
Wrong approach:IF input1 THEN output := TRUE; ELSE IF input2 THEN output := FALSE; END_IF;
Correct approach:IF input1 THEN output := TRUE; ELSE IF input2 THEN output := FALSE; END_IF; END_IF;
Root cause:Forgetting to close all IF blocks properly causes unexpected program flow.
Key Takeaways
IF-THEN-ELSE statements let PLCs make decisions by choosing actions based on conditions.
Always include ELSE or default actions to avoid unpredictable output states.
Combining conditions with AND/OR and nesting IFs allows handling complex logic.
Understanding how IF-THEN-ELSE maps to ladder logic helps in both programming and troubleshooting.
Careful design and testing prevent common bugs like missing ELSE or incorrect nesting.

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