Join us today!

Using AND_THEN , OR...
 
Notifications
Clear all

Using AND_THEN , OR_ELSE vs AND, OR in TwinCAT

2 Posts
2 Users
2 Reactions
1,199 Views
twinControls
Posts: 115
Admin
Topic starter
(@twincontrols)
Member
Joined: 2 years ago

We will discuss the effects of using AND_THEN, OR_ELSE vs AND,OR operators in this tutorial . 

AND , AND_THEN: 

When AND operator is used , all the operands get evaluated for the corresponding condition in TwinCAT. 

IF bCondition1 AND bCondition2 AND ...... THEN ---> TwinCAT evaluates all the conditions 

IF bCondition1 AND_THEN bCondition2 AND_THEN ...... THEN ---> TwinCAT evaluates the bCondition1 first, if it is FALSE, bCondition2 doesn't get evaluated. Other operands gets executed only if the first operand of the AND_THEN is TRUE.

This operand may come in handy when dealing with pointers. Let's check the example below. Add the code in your main program: 

VAR
	pData : PVOID;
	nNumber:POINTER TO INT;
END_VAR
nNumber := pData;
IF pData <> 0 AND nNumber^ = 95 THEN
	nNumber :=5;
END_IF

 

In the code, we have assigned the value of pData to the address of the nNumber variable. Since the address of the nNumber becomes 0, this would cause an exception in TwinCAT when evaluating the nNumber^ = 95 condition. If you execute this code, you would get the exception below: 

and else exception

 

If you use AND_THEN instead, TwinCAT doesn't evaluate (nNumber^ = 95) condition since the evaluation of pData <> 0 is FALSE. Therefore, you would not get an exception in this case. 

and else no exception

 

 

OR, OR_ELSE:

When OR operator is used , all the operands get evaluated for the corresponding condition in TwinCAT. 

IF bCondition1 OR bCondition2 OR ...... THEN ---> TwinCAT evaluates all the conditions 

IF bCondition1 OR_ELSE bCondition2 OR_ELSE ...... THEN ---> TwinCAT evaluates the bCondition1 first, if it is TRUE, bCondition2 doesn't get evaluated. If the first operand of the OR_ELSE is TRUE, other operands are no longer executed. 

 

To see the effect , we have created a method called IncreaseBy and inside this method we are always returning TRUE and we are logging a warning every time the method gets executed. 

FB_Calculate.IncreaseBy():

METHOD IncreaseBy : BOOL
VAR_INPUT
	nNumber : REFERENCE TO INT;
	nIncreaseBy : INT;
END_VAR
nNumber := nNumber + 1;
ADSLOGSTR(ADSLOG_MSGTYPE_WARN, 'Increase By Method executed', '');
IncreaseBy := TRUE;

 

Add the code below in the MAIN program : 

VAR
	bSystemActive : BOOL := TRUE;
	fbCalculate : FB_Calculate;
	bStart : BOOL;
	nNumber : INT;
	
	bDo_1 : BOOL;
	bDo_2 : BOOL;	
END_VAR

IF bDo_1 THEN
	bStart := bSystemActive OR fbCalculate.IncreaseBy(nNumber,1);
	bDo_1 := FALSE;
END_IF 

IF bDo_2 THEN
	bStart := bSystemActive OR_ELSE fbCalculate.IncreaseBy(nNumber,1);
	bDo_2 := FALSE;
END_IF 

 

Every time, this line ;

bStart := bSystemActive OR fbCalculate.IncreaseBy(nNumber,1);

 gets executed, we will get the warning and the number will be increased by 1 since all the conditions get evaluated. 

 

However, in this line ;

bStart := bSystemActive OR_ELSE fbCalculate.IncreaseBy(nNumber,1);

  fbCalculate.IncreaseBy(nNumber,1) gets evaluated only if bSystemActive is FALSE. 

 

 

In this video, we can see that every time we set bDo_1 to TRUE, we would get the warning and we see the increment effect because of IncreaseBy() method getting evaluated. 

When we set bDo_2 to TRUE, we don't get a warning and the number doesn't increase because fbCalculate.IncreaseBy(nNumber,1) doesn't get executed since bSystemActive is already TRUE. 

 

 

Reply
1 Reply
rruiter
Posts: 63
(@rruiter)
Estimable Member
Joined: 2 years ago

Another example from plccoder

Reply
Share: