Bird
Raised Fist0
PLC Programmingscripting~10 mins

CASE statement in PLC Programming - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - CASE statement
Start
↓
Evaluate expression
↓
Match case value?
No→Default case
Yes↓
Execute matched case block
↓
End CASE statement
The CASE statement evaluates an expression, compares it to each case value, executes the matching block, or the default if no match.
Execution Sample
PLC Programming
CASE day OF
  1: action := 'Monday';
  2: action := 'Tuesday';
  ELSE action := 'Other';
END_CASE;
This code sets 'action' based on the value of 'day' using a CASE statement.
Execution Table
StepExpression ValueCase CheckedMatch?Action TakenVariable 'action'
121NoCheck next case''
222YesSet action to 'Tuesday''Tuesday'
3---Exit CASE'Tuesday'
💡 Expression matches case 2, so 'action' is set to 'Tuesday' and CASE ends.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
day2222
action'''''Tuesday''Tuesday'
Key Moments - 2 Insights
Why does the CASE statement stop checking after finding a match?
Once a matching case is found (see Step 2 in execution_table), the CASE executes that block and exits immediately to avoid running other cases.
What happens if no case matches the expression value?
If no case matches, the ELSE block runs if present, otherwise no action is taken. This is shown by the 'Default case' in concept_flow.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'action' after Step 1?
A'' (empty string)
B'Monday'
C'Tuesday'
D'Other'
💡 Hint
Check the 'Variable action' column at Step 1 in execution_table.
At which step does the CASE statement find a matching case?
AStep 1
BStep 2
CStep 3
DNo match found
💡 Hint
Look at the 'Match?' column in execution_table to see when 'Yes' appears.
If 'day' was 5 and no case for 5 exists, what action would be taken?
ASet action to 'Monday'
BSet action to 'Tuesday'
CSet action to 'Other'
DNo change to action
💡 Hint
Refer to the ELSE block in the execution_sample code and concept_flow.
Concept Snapshot
CASE statement syntax:
CASE expression OF
  value1: action1;
  value2: action2;
  ELSE default_action;
END_CASE;

It compares expression to cases, runs matching block, or ELSE if no match.
Full Transcript
The CASE statement starts by evaluating an expression. It then compares this value to each case value in order. If a match is found, it executes the corresponding block and stops checking further cases. If no match is found, it executes the ELSE block if present. For example, if 'day' equals 2, the CASE matches the second case and sets 'action' to 'Tuesday'. Variables change only when a matching case runs. This helps avoid multiple checks and keeps code clear.

Practice

(1/5)
1. What is the main purpose of a CASE statement in PLC programming?
easy
A. To stop the program execution immediately
B. To repeat a set of instructions multiple times
C. To select one action from many based on a variable's value
D. To declare variables for use in the program

Solution

  1. Step 1: Understand the role of CASE statement

    The CASE statement evaluates a variable and chooses one matching action from multiple options.
  2. Step 2: Compare with other options

    Repeating instructions is done by loops, declaring variables is separate, and stopping execution is a different command.
  3. Final Answer:

    To select one action from many based on a variable's value -> Option C
  4. Quick Check:

    CASE statement = Select action by value [OK]
Hint: CASE chooses action based on value, like a traffic signal [OK]
Common Mistakes:
  • Confusing CASE with loops
  • Thinking CASE declares variables
  • Assuming CASE stops program
2. Which of the following is the correct syntax to start a CASE statement in PLC structured text?
easy
A. CASE variable OF
B. SWITCH(variable) {
C. IF variable THEN
D. SELECT variable FROM

Solution

  1. Step 1: Recall PLC structured text CASE syntax

    The correct syntax to start a CASE statement is CASE variable OF.
  2. Step 2: Eliminate other options

    SWITCH(variable) { is JavaScript style, C is IF statement, D is invalid syntax.
  3. Final Answer:

    CASE variable OF -> Option A
  4. Quick Check:

    PLC CASE starts with 'CASE variable OF' [OK]
Hint: PLC CASE always uses 'CASE variable OF' to start [OK]
Common Mistakes:
  • Using SWITCH instead of CASE
  • Confusing IF with CASE
  • Writing SELECT instead of CASE
3. Given the following PLC code snippet, what will be the value of output after execution?
CASE input OF
  1: output := 'One';
  2: output := 'Two';
  3: output := 'Three';
ELSE
  output := 'Other';
END_CASE;

If input is 2?
medium
A. 'One'
B. 'Two'
C. 'Three'
D. 'Other'

Solution

  1. Step 1: Match input value with CASE options

    The input is 2, so the CASE matches the line 2: output := 'Two';.
  2. Step 2: Determine output value

    Since input is 2, output is assigned the string 'Two'.
  3. Final Answer:

    'Two' -> Option B
  4. Quick Check:

    Input 2 matches case 2, output = 'Two' [OK]
Hint: Match input to CASE label to find output [OK]
Common Mistakes:
  • Choosing ELSE output for matching case
  • Confusing input 2 with 1 or 3
  • Ignoring ELSE clause
4. Identify the error in this CASE statement snippet:
CASE status OF
  0: action := 'Stop';
  1: action := 'Start';
  2 action := 'Pause';
ELSE
  action := 'Unknown';
END_CASE;
medium
A. Missing colon after 2 in third case
B. ELSE clause is not allowed in CASE
C. END_CASE should be ENDCASE
D. Variable 'action' is not declared

Solution

  1. Step 1: Check syntax of each CASE line

    The third case line is missing a colon after 2; it should be 2: action := 'Pause';.
  2. Step 2: Verify other parts

    ELSE clause is valid, END_CASE is correct syntax, and variable declaration is not shown but not an error here.
  3. Final Answer:

    Missing colon after 2 in third case -> Option A
  4. Quick Check:

    Each case label needs a colon ':' [OK]
Hint: Each case label must end with ':' [OK]
Common Mistakes:
  • Omitting colon after case value
  • Thinking ELSE is invalid
  • Confusing END_CASE syntax
5. You want to use a CASE statement to control a motor speed setting based on an integer input from 0 to 4, where 0 means 'Stop', 1 means 'Slow', 2 means 'Medium', 3 means 'Fast', and any other value means 'Error'. Which of the following CASE statements correctly implements this logic?
hard
A. CASE speed OF 0: motor_speed := 'Stop'; 1: motor_speed := 'Slow'; 2: motor_speed := 'Medium'; 3: motor_speed := 'Fast'; END_CASE;
B. CASE speed OF 0: motor_speed := 'Stop'; 1: motor_speed := 'Slow'; 2: motor_speed := 'Medium'; 3: motor_speed := 'Fast'; 4: motor_speed := 'Error'; END_CASE;
C. CASE speed OF 0: motor_speed := 'Stop'; 1: motor_speed := 'Slow'; 2: motor_speed := 'Medium'; 3: motor_speed := 'Fast'; 4: motor_speed := 'Error'; ELSE motor_speed := 'Error'; END_CASE;
D. CASE speed OF 0: motor_speed := 'Stop'; 1: motor_speed := 'Slow'; 2: motor_speed := 'Medium'; 3: motor_speed := 'Fast'; ELSE motor_speed := 'Error'; END_CASE;

Solution

  1. Step 1: Check the required mapping

    Values 0 to 3 have specific motor speeds; any other value should set 'Error'.
  2. Step 2: Analyze each option

    CASE speed OF 0: motor_speed := 'Stop'; 1: motor_speed := 'Slow'; 2: motor_speed := 'Medium'; 3: motor_speed := 'Fast'; ELSE motor_speed := 'Error'; END_CASE; uses ELSE for all other values, correctly handling unexpected inputs. CASE speed OF 0: motor_speed := 'Stop'; 1: motor_speed := 'Slow'; 2: motor_speed := 'Medium'; 3: motor_speed := 'Fast'; 4: motor_speed := 'Error'; END_CASE; treats 4 as 'Error' but misses ELSE for other values. CASE speed OF 0: motor_speed := 'Stop'; 1: motor_speed := 'Slow'; 2: motor_speed := 'Medium'; 3: motor_speed := 'Fast'; 4: motor_speed := 'Error'; ELSE motor_speed := 'Error'; END_CASE; incorrectly uses a colon after ELSE ('ELSE:'), which is invalid syntax. CASE speed OF 0: motor_speed := 'Stop'; 1: motor_speed := 'Slow'; 2: motor_speed := 'Medium'; 3: motor_speed := 'Fast'; END_CASE; lacks handling for values outside 0-3.
  3. Final Answer:

    CASE speed OF 0: motor_speed := 'Stop'; 1: motor_speed := 'Slow'; 2: motor_speed := 'Medium'; 3: motor_speed := 'Fast'; ELSE motor_speed := 'Error'; END_CASE -> Option D
  4. Quick Check:

    Use ELSE for unexpected values in CASE [OK]
Hint: Use ELSE to catch all unexpected values in CASE [OK]
Common Mistakes:
  • Not using ELSE for unexpected values
  • Assigning error to a specific case instead of ELSE
  • Missing cases for valid inputs
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