×

Subscribe to newsletter

subscribe to our weekly newsletter to get notified with latest story and article Subscribe now!




PLC BLOG | PLC programming - structured text conditional statements
Share on Facebook Share On Twitter Share on LinkedIn Share on Whatsapp


PLC programming - structured text conditional statements

structured text conditional statements are used to start a process or system at specific condition if condition is true. Conditional statements is decisions based statement on certain process condition. Fundamental of conditional statements is compare two or more than two values and execute statements if condition is true. if comparison result true than the result is 1 and if the result is false than the result is 0. In programming languages 0 represent false condition and 1 represent true condition. Conditional statements help to make a program for different decisionsconditional statements used Relational operators for comparison and if you have multiple condition than used logiacal operators. in structured text have following conditional statements


Structured Text IF_THEN Condition Statements

IF_THEN condition statements execute code if the condition is true. IF_THEN condition statements is very simple. Expression of IF_THEN is
  • if (condition)
  • then
  • 'code to execute if the condition is true';
  • end_if;
Below is a example of IF_THEN statements so consider a example of two tank filling system with two electrical motor pump one pump for each motor. so in our process we have two tank, two water level transmitter for reading water level of tank, two electrical pump with simple start stop.

structured text IF_THEN condition statements

  • so we use water level transmitter for feedback of water level so in our condition electrical pump start if the tank water level is below to tank max level or less than or equal tank max level meter.
  • when pump start tank water level start and reached to max level so we have another condition for stop pump . in this condition when water level is greater than or equal to max level than water pump stop.
So this code is very simple to just start and stop of motor. So we take next condition and add some more condition to our process so you can understand the conditional statements because it is very important in plc and process.


Structured Text IF_THEN_ELSE Condition Statements

in IF_THEN_ELSE condition statements, code is executed in both true and false condition. while IF_THEN condition statement code executed only in true condition. Expression for IF_THEN_ELSE is

IF_THEN condition statements execute code if the condition is true. And if condition is false then else code executed. Expression of IF_THEN_ELSE is
  • if (condition)
  • then
  • 'code to execute if the condition is true';
  • ELSE
  • 'code to execute if the condition is false';
  • end_if;

structured text IF_THEN_ELSE condition statements

in above example of IF_THEN_ELSE condition when pump start than pump_start_1_ind_lamp is set to 1 and your indicator lamp will on and predefined alarm list(alarm_list[30]) move to tank_lvl_1_alarm.DATA[1]. if pump not start than pump_start_1_ind_lamp is set to 0 and lamp off, and an error alarm generate error (set of error_alarm move to tank_lvl_1_alarm).The [:=] tells the controller to change value if values already set.


Structured Text IF_THEN_ELSIF Condition Statements

IF_THEN_ELSIF is multiple if condition. If first condition is true than first code is executed, if first condition is false and second condition is true than second code executed and if all two condition is false and third if condition is true than third statement is execute and so on. You can use multiple set of if statements and condition. Expression for IF_THEN_ELSIF statement is
  • if (condition)
  • then
  • 'first code to execute if the condition is true';
  • ELSIF(condition)
  • 'second code to execute if the first condition is false and second condition is true';
  • end_if;

below is the two example of IF_THEN_ELSIF conditional statement. So in our process tank have maximum level 3 meter so we have a condition if the tank level below 1.2 meter than tank low alarm is to set, if tank level is greater than 1.2 and less than 2.2 than alarm set tank filled if tank level is greater than 3 meter than high alarm is set. So our code look like this

structured text IF_THEN_ELSIF condition statements

here i am using multiple if statements to show how it works you can make your own logic if found any problem than please share with us.


Structured Text IF…THEN…ELSIF…ELSE Condition Statements

IF…THEN…ELSIF…ELSE is nested if then else condition if first condition is true than first code is executed, if first condition is false and second condition is true than second code executed and so on. And if all above condition is false than else condition is executed. You can use multiple set of if statements and condition. Expression for IF…THEN…ELSIF…ELSE statement is
  • if (condition)
  • then
  • 'first code to execute if the condition is true';
  • ELSIF(condition)
  • 'second code to execute if the first condition is false and second condition is true';
  • ELSE
  • 'code to execute if the all above condition is false';
  • end_if;

as above example i am simply add else condition if all the condition is false than else condion is excuted

structured text IF…THEN…ELSIF…ELSE condition statements

if else is important role in programming so please do more practice. I tried to explain as much as possible. Without logical operators conditional statements is not complete we discuss on logical operators in next article. All example are sample code.


Share on Facebook Share On Twitter Share on LinkedIn Share on Whatsapp


Suggested Post


 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

comment