What if....

Peter Haworth pete at lcsql.com
Sun Jan 27 14:27:42 EST 2013


Interesting on the use of parens in this situation.  I guess it makes sense
it works the way it does but it doesn't really come across that way in the
manual.
Pete
lcSQL Software <http://www.lcsql.com>


On Sun, Jan 27, 2013 at 10:53 AM, Mike Bonner <bonnmike at gmail.com> wrote:

> ifs are evaluated left to right, though there is some leeway using parens.
>
> As to your question though, yes putting slower evals at the end of a string
> of ands and ors does save time if the if fails before the slow parts.
>
> Did the following in a button to test. Just change one of the first
> conditions to be false and the long myfunc call is never hit.  Placed
> parens around the myfunc() to demonstrate that they don't give precedence
> in this situation.
>
> *on* mouseUp
>
>    *if* 1 is 1 and 2 is 2 and (myfunc()) *then* *put* "It's all true"
>
> *end* mouseUp
>
>
> *function* myFunc
>
>    *repeat* 10000 times
>
>       *put* random(10)
>
>    *end* *repeat*
>
>    *if* random(2) is 1 *then*
>
>       *return* true
>
>    *else*
>
>       *return* false
>
>    *end* *if*
>
> *end* myFunc
>
>
>
> On Sun, Jan 27, 2013 at 11:39 AM, Peter Haworth <pete at lcsql.com> 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
> >
> _______________________________________________
> 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