In this example Coils[0] will latch the result in a true evaluation, if you want it to behave the same way as example 1 then you need to do it like example 3 below:
However back to the real question. There is no problem using either example 1 or example 3, if im not requiring a latch i usually go with example 1 as it is more compact.
π
1
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello
Can anyone see any problems with constant assignment as in the following statement:
Coils[0] := Coils[0] OR (Buttons[0] AND Sensors[0] > 6);
Rather than using the more verbose and vertically-larger:
Also, is the test expression short-circuited in the latter case? In other words, will the runtime move on after it sees
Coils[0] = FALSE AND
?Thanks
Its important that you know the difference between the two.
Example 1:
In this case the statements will be evaluated every scan and result in either true or false for Coils[0].
Example 2:
In this example Coils[0] will latch the result in a true evaluation, if you want it to behave the same way as example 1 then you need to do it like example 3 below:
However back to the real question. There is no problem using either example 1 or example 3, if im not requiring a latch i usually go with example 1 as it is more compact.
You can consider using OR_ELSE:
Coils[0] := Coils[0] OR_ELSE (Buttons[0] AND Sensors[0] > 6);
https://content.helpme-codesys.com/en/CODESYS%20Development%20System/_cds_operator_or_else.html
IF Coils[0] is TRUE, then the rest is not relevant anymore and doesn't have to be evaluated anymore.
PS, there is also an AND_THEN
https://content.helpme-codesys.com/en/CODESYS%20Development%20System/_cds_operator_and_then.html
Thanks for your responses. I like to write the leanest possible code that I can because it makes it much easier to read/maintain/debug.
OR_ELSE
is what I was looking for. I wasn't sure if CodeSys short-circuited evaluation and I didn't realize this operator existed. Thanks!