A curious case

Nonsanity form at nonsanity.com
Wed Mar 2 10:40:52 EST 2011


Others have answered this pretty well, but I thought I'd add some examples
for clarity - With future readers in mind.

The following won't work:

if x = 1 then
    doSomething
else if x = 2 then
    if y = 3 then doSomethingElse
else if x = 2 then  -- little lost else
    doThirdThing
end if

This is because the y = 3 line thinks the next else belongs to it, like so:

if x = 1 then
    doSomething
else if x = 2 then
    if y = 3 then doSomethingElse
    else if x = 2 then
       doThirdThing
    end if

Because the following is a valid if-else format:

    if true then Blah1
    else Blah2

If there's another line between the dangling if and the parent structure's
next else, all is fine.

if x = 1 then
    doSomething
else if x = 2 then
    if y = 3 then doSomethingElse
    get it
else if x = 2 then
    doThirdThing
end if

But the best practice, as Terry said, is to use the long form in situations
like these.

if x = 1 then
    doSomething
else if x = 2 then
    if y = 3 then
        doSomethingElse
    end if
else if x = 2 then
    doThirdThing
end if

That way the code is clear to everyone, at least in regard to
else-ownership. Clarity of the code itself varies by programmer... :)


 ~ Chris Innanen
 ~ Nonsanity



On Tue, Mar 1, 2011 at 7:40 PM, Bob Sneidar <bobs at twft.com> wrote:

> Hi all.
>
> I just came across a curious issue where I had an if then else control
> structure inside another if then structure.
>
> I *thought* I used toe be able to use the form
>
> if statement then
>    -- do somestuff
> else if anotherstatement then
>    -- do someotherstuff
> else
>    -- do defaultstuff
> end if
>
> When I nested this inside another if then else control structure it told me
> I was missing an end if! However if I converted the above structure to a
> switch control structure the script compiled fine. Have I stumbled upon
> something here? It kind of makes sense to me because the compiler may be
> having trouble knowing which control structure *else if anotherstatement*
> belongs to.
>
> This is not a problem per se, but I am just curious and it may help someone
> else who encounters it in the future to not spend an hour or two poring over
> code that is technically correct but won't compile.
>
> Bob
>
>
>
> _______________________________________________
> 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