In the previous post, we saw how to write an if else statement in SCL language. The next construct we are going to use is case statement. This Case statement is used widely for selection-based programs, which helps in reducing code size if an if else statement was used. So, studying case statements is a very integral part of learning ST language. In this post, we will see the use of case statements in SCL language.
What is a case statement?
A case statement is a construct which is used to execute statements inside it based on the various types of cases or values. That means, if there are 10 cases, then when a current case value matches the set case value, the corresponding statements inside that set case are executed. This is useful when you have various criteria and want to execute a criteria depending on the current condition. If case statement is not used, then you can use an if else statement too here, but the program coding will be increased and also the complexity.
The general syntax of case statement is :
Case var of
1: statement;
2: statement;
3: statement;
..
N: statement;
end_case;
Here, the var refers to a variable which takes a value and depending on that value, the statements matching that case value are executed. Means if the current value is 2, then the statements under case-2 will be executed.
How to write a case statement in SCL language?
Now that we have understood the basics, let us write a sample logic using a case statement. Refer to the below image, where we compare it with an if else statement. The logic is – if the value of variable I_Pv is 34, then we add 1 with the value of var_2 and move the answer to var_1; and if the value of variable I_Pv is 35, then we add 1 with the value of var_3 and move the answer to var_1.

In the first scenario, we used an if-else statement. In the first if-else construct, we write the logic for comparing I_Pv with 34. In the second if-else construct, we write the logic for comparing I_Pv with 35. Then, whichever condition is true, the corresponding addition is done inside them. Due to this, our logic is executed.
But if we see the second scenario, we used a case statement. Here, as per the general syntax mentioned earlier, we use the case of I_Pv as reference value. The statement monitors the current value of this variable, and if the value is 34, then the addition written under 34: is executed; and if the value is 35, then the addition written under 35: is executed. Then, we terminate the construct with the end_casekeyword.
If there are a large number of such comparisons, then instead of writing an if or if else multiple times, we can simply use a case statement for solving the issue. Our coding size reduces and the program too looks easier to troubleshoot.
In the next post, we will see how to use a for do statement in SCL language.
Read Next:
- Types of Expressions in the SCL Language
- Mail Box Automation: Programming with PLC
- PLC Hoist Crane Programming Example
- PLC Mixing Process with Timer and Valve Control
- PLC Programming for Loss in Weight Liquid Systems