What if....

Peter M. Brigham pmbrig at gmail.com
Tue Jan 29 13:45:20 EST 2013


One situation when the order of evaluation is important is if you have conditions that limit the scope of a general handler, eg:

if "field" is in the target and the locktext of the target = true then…

In this case the first clause is evaluated first, and if the target is something other than a field, the second clause is skipped and you avoid throwing an error. With the clauses switched in order, you throw an error if the target is, eg, a button.

I used to nest if statements:

if "field" is in the target then
   if the locktext of the target = true then…

... until I realized I could do it all in one line.

-- Peter

Peter M. Brigham
pmbrig at gmail.com
http://home.comcast.net/~pmbrig

On Jan 27, 2013, at 1:39 PM, Peter Haworth wrote:

> Musings about complex if statements on this Sunday morning....
> 
> Lets say you have a complex if statement with 4 conditions that must all be
> true.  Is there any advantage, other than personal preference/style, to:
> 
> if cond1 and cond2 and cond3 and cond4 then.....
> 
> or
> 
> if cond1 then
>   if cond2 then
>      if cond3 then
>         if cond4 then...
> 
> In either case, as soon as a condition evaluates to false, none of the rest
> are evaluated, right?  If so, then always best to put the conditions that
> take the longest to evaluate further down the list, for example perhaps an
> SQL SELECT statement that might take a while to execute?  But then what if
> some of the conditions are in parens, for example:
> 
> if cond1 and cond2 and (cond3 and cond4) then....
> 
> The manual says the grouping operator (parens) has the highest precedence
> so does this mean (cond3 and cond4) will be evaluated before cond1 and
> cond2?
> 
> Just curious.
> 
> 
> Pete
> lcSQL Software <http://www.lcsql.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





More information about the Use-livecode mailing list