Skip to content
Snippets Groups Projects
Commit 8255d2dc authored by benjamin.franksen's avatar benjamin.franksen
Browse files

docs: added a note regarding lazy evaluation of logical operators

parent 13b98ed4
No related branches found
No related tags found
No related merge requests found
...@@ -1042,6 +1042,13 @@ Left-associative Binary Operators ...@@ -1042,6 +1042,13 @@ Left-associative Binary Operators
expr: `expr` "&" `expr` expr: `expr` "&" `expr`
expr: `expr` "%" `expr` expr: `expr` "%" `expr`
.. note:: Like in most programming languages, evaluation of conditional
expressions using ``&&`` and ``||`` is done *lazily*: the second operand
is not evaluated if evaluation of the first already determines the
result. This particularly applies to the boolean expressions in
:token:`transition` clauses. See the built-in function :c:func:`pvGetQ`
for an extended discussion.
Ternary Operator Ternary Operator
~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~
...@@ -1461,22 +1468,21 @@ combining a call to pvGetQ with other conditions in the same ...@@ -1461,22 +1468,21 @@ combining a call to pvGetQ with other conditions in the same
} state ... } state ...
would remove the head from the queue every time the condition gets would remove the head from the queue every time the condition gets
evaluated, regardless of whether ``other_condition`` is ``TRUE``. This is most evaluated, regardless of whether ``other_condition`` is ``TRUE``. This is
probably not the desired effect, as you would lose an most probably not the desired effect, as you would lose an unknown number of
unknown number of messages. (Of course it *could* be exactly what you want, messages. (Of course it *could* be exactly what you want, for instance if
especially if ``other_condition`` were in fact ``!suppress_output``.) ``other_condition`` were something like ``!suppress_output``.) Whereas ::
Whereas ::
when (other_condition && pvGetQ(msg)) { when (other_condition && pvGetQ(msg)) {
printf(msg); printf(msg);
} state ... } state ...
is "safe", in the sense that no messages will be lost. BTW, If you is "safe", in the sense that no messages will be lost. BTW, If you combine
combine with a disjunction ("||") it is the other way around, i.e. pvGetQ with a disjunction ("||") it is the other way around, i.e. pvGetQ should
should appear as the first operand. This is all merely a result of the appear as the first operand. This is all merely a result of the evaluation
evaluation order imposed by the C language; similar remarks apply order imposed by the C language (and thus by SNL); similar remarks apply
whenever you want to use an expression inside a :token:`transition` clause that whenever you want to use an expression inside a :token:`transition` clause
potentially has a side-effect. that potentially has a side-effect.
pvFreeQ pvFreeQ
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment