As seen in earlier posts, one of the most used constructs in SCL language is an if else statement. This is the most used statement in ST language of overall automation platforms. What makes it popular is its relative simplicity in defining them. If a programmer at least learns this construction only, then too he can make a whole program in SCL language. In this post, we will see how we can write different styles in an if else statement.
What is the if else and if statement?
As the name describes, an if else statement is a construction which checks the condition first and then executes a statement inside it if true. Or else, it will execute another statement if false. So either way, it will execute something or the other. A simple statement apart from if else is an only if statement. In this case, if the condition is true, then only the statement inside it will execute. Otherwise, it will skip the whole construct and go to the next line after end_if line. So, you can either use if or use if else statements according to your requirement. We will see the use of both cases in the next part.
How to write an if statement?
Let us see the first style on how to write a simple if statement. Refer to the below image. As defined above, if the condition is true, then the statements inside it will execute. Otherwise, it will leave and go to the next case. In our example, we use the same ladder logic of start and stop. The logic written is – if the variable I_Start is on, then the variable Sys_Running turns on. If the variable I_Stop is on, then the variable Sys_Running turns off. So, as in the ladder logic, we need to latch the system running bit, because the start button is momentary. That is why, we write the logic below.

Now, if we see an if statement written in SCL language, we use two such types. In the first case, if the start variable is on, then we write value “1” to the sys_running bit. If the start variable was not on, then the statement inside it would not execute. But here, we also need to turn it off when the stop button is pressed. So in the second case, if the stop variable is on, then we write value “0” to the sys_running bit. If the stop variable was not on, then the statement inside it would not execute. Due to this, our logic is executed properly according to our requirements. So, the step to write an if statement is – write the condition inside if keyword, use then keyword, write the statements inside it, and use end_if keyword to terminate.
How to write an if else statement?
Let us see the second style on how to write a simple if-else statement. Refer to the below image. As defined above, if the condition is true, then the statements inside it will execute. Otherwise, it will execute the other statements and go to the next case. In our example, we use the same ladder logic of start and stop. The logic written is – if the variable I_Start is on, then the variable Sys_Running turns on. If the variable I_Stop is on, then the variable Sys_Running turns off. So, as in the ladder logic, we need to latch the system running bit, because the start button is momentary. That is why, we write the logic below.

Now, if we see an if-else statement written in SCL language, we use one such type. If the start variable is on or the sys_running bit is on and the stop variable is off, then we write value “1” to the sys_running bit. Else, we write value “0” to that same bit. So, in either case, the corresponding statements are executed. Due to this, our logic is working properly according to our requirements. So, the step to write an if-else statement is – write the condition inside if keyword, use then keyword, write the statements inside it, then use else keyword, write the statements inside it, and use end_if keyword to terminate.
In both the styles, the work done is the same. Only the way of writing differs. For this purpose, you need to understand the question first in any case, which will help you write the logic in any scenario. In the next post, we will see how to use case statements in SCL language.
Read Next:
- Types of Expressions in the SCL Language
- How to Configure IP Address in Rockwell PLC?
- Complex Car Parking Logic in Electric PLC
- PLC Bottle’s Capping with a Rotating Mechanism
- PLC Programming for Sorting Conveyor System