Cookies are important for this site to function properly, to guarantee your safety, and to provide you with the best experience. By clicking OK, you accept all cookies. For more information, please access our Privacy Policy.
Table of Contents
Tutorials / 
How to Use CASE Statement in SCL for Efficient PLC Programming
Intermediate

How to Use CASE Statement in SCL for Efficient PLC Programming

Siemens
TIA Portal
PLC Programming
Structured Control Language (SCL)

Introduction

Efficiency and precision are paramount in industrial automation, and the CASE statement stands out as a critical tool for achieving both. Why learn it? Unlike traditional IF-THEN-ELSE constructs, the CASE statement simplifies complex decision-making by elegantly handling multiple conditions with a single, streamlined instruction. Not only does the CASE statement reduce code clutter, but it also boosts readability and maintainability—key factors in time-sensitive projects. By reading this tutorial, you'll unlock the ability to craft cleverer, more adaptable programs that respond dynamically to varying inputs. Ahead, you'll discover the CASE statement's structure, execution flow, and versatile parameters, from integer ranges to bit string constants. We'll dive into a practical example, showing how it controls outputs based on a counter's value and exploring nesting for advanced logic. Whether you're a beginner or a seasoned coder, this guide promises actionable insights to elevate your SCL programming prowess. Let's get started!

Figure 1.1: CASE statement in SCL - Structure of CASE statement in programming
Figure 1.1: CASE statement in SCL - Structure of CASE statement in programming

Prerequisites

Before engaging with this tutorial, check that the TIA Portal software is operational on your PC. Although this guide is based on version 19, other variants are perfectly viable. It's equally necessary to possess a solid grasp of "SCL Programming Basics" and "SCL Instructions" too.

CASE Statement Description

The CASE instruction, commonly recognized as "Create Multiway Branch," is designed to execute one specific sequence of instructions from a collection of several options, depending entirely on the value produced by a given expression. The result of this expression must always be either an integer or a bit string format. When the CASE instruction begins its execution process, it systematically compares the value of the expression, referred to as the tag, with multiple predefined constants. If the tag's value matches any one of these constants precisely, the condition is fully satisfied, and the instructions programmed immediately following that particular constant are carried out. The constants themselves may encompass a broad range of diverse values.

Figure 2.1: CASE statement in SCL - CASE statement flowchart
Figure 2.1: CASE statement in SCL - CASE statement flowchart

CASE Statement Execution Flow

When the value produced by the expression matches the first constant (<Constant1>) in the sequence, the set of instructions (<Instructions1>) that directly follows this constant is executed immediately, after which the program flow naturally continues beyond the "END_CASE" marker, resuming ordinary operation.

Figure 3.1: CASE statement in SCL - Immediate action & flow continuation
Figure 3.1: CASE statement in SCL - Immediate action & flow continuation

However, if the expression's value does not correspond to the <Constant1>, the CASE statement continues evaluating it against the next constant listed in the CASE instruction's structure. This comparison process repeats systematically, moving through each subsequent constant in an orderly fashion until a match is identified.

Figure 3.2: CASE statement in SCL - Systematic constant checks
Figure 3.2: CASE statement in SCL - Systematic constant checks
Figure 3.3: CASE statement in SCL - CASE statement iterative evaluation
Figure 3.3: CASE statement in SCL - CASE statement iterative evaluation

Should the expression's value fail to align with any constants explicitly programmed within the CASE block, the instructions (<Instructions0>) positioned after the ELSE clause are carried out as a fallback mechanism.

Figure 3.4: CASE statement in SCL - Using ELSE as a CASE fallback
Figure 3.4: CASE statement in SCL - Using ELSE as a CASE fallback

It's worth noting that the ELSE portion is not a mandatory component and can be excluded entirely from the syntax if deemed unnecessary.

Figure 3.5: CASE statement in SCL - Non-mandatory ELSE in CASE statement
Figure 3.5: CASE statement in SCL - Non-mandatory ELSE in CASE statement

Furthermore, the CASE instruction offers flexibility through nesting, allowing a programmer to substitute an instruction block with another CASE statement for more complex logic. The 'END_CASE' keyword serves as the definitive endpoint of the entire CASE instruction framework.

Figure 3.6: CASE statement in SCL - Nested CASE statement
Figure 3.6: CASE statement in SCL - Nested CASE statement

CASE Statement Parameters

This tutorial segment invites you to delve deeply into the parameters that define the CASE statement, starting with considering the "<Tag>." This crucial element, known as the expression, carries a value that must be either an integer or a bit string, setting the stage for subsequent comparisons. The CASE statement evaluates this "<Tag>" by measuring it against a collection of programmed constants within the CASE framework.

Figure 4.1: CASE statement in SCL - CASE expression/tag & data type rules
Figure 4.1: CASE statement in SCL - CASE expression/tag & data type rules

Next in line is the <Constant> parameter, which offers a broad scope for customization. These constants can originate locally within the specific block or extend globally throughout the program, adapting to the programmer's needs. Their data type is the same as the "<Tag>," allowing them to manifest as integers or bit strings.

Figure 4.2: CASE statement in SCL - Constants scope & data type matching
Figure 4.2: CASE statement in SCL - Constants scope & data type matching

Constants facing bit strings might be expressed in diverse formats, such as binary notation like 2#10, an octal figure like 8#77, or a hexadecimal code like 16#AD. They could also appear as untypified constants, such as 1000, providing a simple yet effective option.

Figure 4.3: CASE statement in SCL - Representing bit strings
Figure 4.3: CASE statement in SCL - Representing bit strings

For typified tags, the constants adjust to the tag's nature. A byte tag requires byte constants like BYTE#2, while a Word tag permits the usage of byte or word constants, such as BYTE#2 or WORD#2. With a DWord tag, the possibilities expand to include byte, word, or DWord constants, illustrated by BYTE#2, WORD#2, or DWORD#2. An LWord tag takes this further, accommodating constants of byte, word, DWord, or LWord, like BYTE#2, WORD#2, DWORD#2, or LWORD#2.

Figure 4.4: CASE statement in SCL - Constants for typified tags
Figure 4.4: CASE statement in SCL - Constants for typified tags

When working with integers, constants can be a single number like 5, a span such as 15 to 20 (15...20), or an enumeration blending individual integers and ranges, such as 10, 11, or 15 to 20 (10, 11, 15...20).

Figure 4.5: CASE statement in SCL - Integer constants and range syntax
Figure 4.5: CASE statement in SCL - Integer constants and range syntax

Lastly, the <Instruction> parameter enters the picture, encompassing any operation triggered when the "<Tag>" aligns with a constant, except for ELSE instructions, which execute only in the absence of a match.

Figure 4.6: CASE statement in SCL - Executing instructions for matched constants
Figure 4.6: CASE statement in SCL - Executing instructions for matched constants

Application Example of CASE Statement

In this application example, you explore how a counter's value dictates which outputs activate within an automation system. The logic is elegantly simple yet powerful. When the counter registers a value of 0, the first output is energized, illuminating the system's response. Should the counter's value fall within the range of 1 to 4, the second output takes charge and activates seamlessly. Alternatively, if the counter reaches the specific values of 6, 8, or 10, the third output springs into action, delivering a targeted response.

Figure 5.1: CASE statement in SCL - Application example overview of CASE statement
Figure 5.1: CASE statement in SCL - Application example overview of CASE statement

According to Figure 5.3, an incremental counter has been created from lines 1 to 5. When the "Counter_Up" tag is activated, the counter increments by one unit. Once the "Counter_Reset" tag is activated, the counter's value is reset. Ultimately, the counted value is stored in the "Counter_Value" tag.

Figure 5.2: CASE statement in SCL - IEC counter data block & tags of the application example
Figure 5.2: CASE statement in SCL - IEC counter data block & tags of the application example
Figure 5.3: CASE statement in SCL - Creating an incremental counter
Figure 5.3: CASE statement in SCL - Creating an incremental counter

Lines #7, #8, and #9 are initialization statements that set the values of Output_1, Output_2, and Output_3 to FALSE before the CASE statement evaluates the counter value. The CASE statement doesn't explicitly set the other outputs to FALSE. It only assigns TRUE to the output result that matches the condition and leaves the rest untouched. By resetting all outputs to FALSE beforehand, you ensure that only the output corresponding to the current CV value is TRUE, and the others are guaranteed FALSE. It creates a clean, mutually exclusive logic where only one output (or none) is active at a time.

Figure 5.4: CASE statement in SCL - Guaranteeing single output activation
Figure 5.4: CASE statement in SCL - Guaranteeing single output activation

In lines #11 to #18, the CASE statement is used, and it is instructed to execute particular commands based on the value counted by the counter ("Counter_Value").

Figure 5.5: CASE statement in SCL - Executing commands based on counter values
Figure 5.5: CASE statement in SCL - Executing commands based on counter values

Lines #12 and #13 specify that if the counter value is 0, the first output ("Output_1") should be energized.

Figure 5.6: CASE statement in SCL - Zero-count trigger for output activation
Figure 5.6: CASE statement in SCL - Zero-count trigger for output activation

Lines #14 and #15 state that if the counter value is between 1 and 4, the second output ("Output_2") should be turned on.

Figure 5.7: CASE statement in SCL - Range-based output control
Figure 5.7: CASE statement in SCL - Range-based output control

Finally, lines #16 and #17 indicate that if the counter value is 6, 8, or 10, the "Output_3" should be activated.

Figure 5.8: CASE statement in SCL - Discrete counter values driving specific output
Figure 5.8: CASE statement in SCL - Discrete counter values driving specific output

As line #16 displays, you can write multiple values on a single line and separate them with commas. So, if the variable matches any of the values specified on that line, the CASE statement will execute the corresponding command. Additionally, as shown in line #14, you can write a range of values on a single line and define this range using the ".." symbol.

Figure 5.9: CASE statement in SCL - Compact CASE syntax for ranges and lists
Figure 5.9: CASE statement in SCL - Compact CASE syntax for ranges and lists

Conclusion

In conclusion, you learned how to effectively utilize the CASE statement in SCL for streamlined PLC programming within the TIA Portal. This powerful instruction enables you to create multiway branches, directing program flow based on an expression’s integer or bit string value. You explored its execution process, where the tag is compared against predefined constants, triggering specific instructions when a match occurs or defaulting to the optional ELSE clause when no match is found. You also discovered the flexibility of the CASE statement, including its ability to handle diverse constant formats— from binary and hexadecimal to ranges and enumerations—and its support for nested logic. Through exploring the application example, you figured out how a counter’s value can selectively activate outputs, reinforcing the importance of initializing variables for clean, mutually exclusive outcomes. This tutorial equipped you with the knowledge to implement CASE statements confidently, enhancing your ability to design efficient, logical, and adaptable automation solutions. With this foundation, you’re well-prepared to tackle complex programming challenges in industrial environments.