Evaluation of complex conditions
Bob Sneidar
bobs at twft.com
Thu Oct 6 17:44:14 EDT 2011
Right logical expressions are left to right. I may have been confused on the issue. There would be no advantage to doing it any other way so far as I can see.
Bob
On Oct 6, 2011, at 2:13 PM, Pete wrote:
> Thanks Graham. Yes, someone else provided an example of the function you
> mentioned to illustrate how things work. But I cannot reproduce the
> behavior mentioned when conditions are enclosed in parens - the order of
> evaluation doesn't seem to be affected.
>
> I wrote a couple of functions (condition1 and condition2), each of which
> puts its identity after a script variable named myResult then returns true.
> I then wrote the following:
>
> put empty into myResult
> if condition1() is true and condition2() is true then put myResult
>
> As expected, this displayed 12 in the message box.
>
> Then I changed the if statement to :
>
> if condition1() is true and (condition2() is true) then put myResult
>
> The message box still ended up with 12 in it, not 21 as you would expect if
> the parens had any effect. Maybe I'm misunderstanding something but it
> doesn't seem like parens have any effect on the order of evaluation.
>
> Pete
> Molly's Revenge <http://www.mollysrevenge.com>
>
>
>
>
> On Thu, Oct 6, 2011 at 1:01 PM, Graham Samuel <livfoss at mac.com> wrote:
>
>> Yes, in any 'and' where the first condition is false, then the second
>> condition is not evaluated; similarly in any 'or', if the first condition is
>> true, the second isn't evaluated. Parentheses alter the order of evaluation
>> as others have said.
>>
>> It's easy to prove this by writing a script where the thing generating your
>> condition ('a' in your example) is a function - you can then make the
>> function have a side effect. As sample function might be
>>
>> function ReturnsTrue
>> put "condition returning true has been invoked;" after whatHappened
>> return true
>> end ReturnsTrue
>>
>> you then write an 'if' statement which at some point in the logic calls
>> ReturnsTrue(), and when you look at the global 'whatHappened' you will see
>> whether the evaluation reached that point or not...
>>
>> Graham
>>
>>
>>
>> On 5 October 2011 16:25, Pete <pete at mollysrevenge.com> wrote:
>>
>>> No, I mean:
>>>
>>> If a is true then
>>> if (b is true or c is true) then
>>> do whatever
>>> end if
>>> end if
>>>
>>> I'm pretty sure if a was false, the second condition wouldn't be
>> evaluated.
>>>
>>> But you raise an interesting point - in your example, does the condition
>>> inside the parens truly get evaluated first?
>>>
>>> Pete
>>> Molly's Revenge <http://www.mollysrevenge.com>
>>
>>
>> _______________________________________________
>> use-livecode mailing list
>> use-livecode at lists.runrev.com
>> Please visit this url to subscribe, unsubscribe and manage your
>> subscription preferences:
>> http://lists.runrev.com/mailman/listinfo/use-livecode
>>
>>
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
More information about the use-livecode
mailing list