Dangling else (was: nested ifs)

Eric Chatonet eric.chatonet at sosmartsoftware.com
Wed Jan 25 10:36:17 EST 2006


Hi Paul,

Here is the copy of a mail I exchanged with Chipp off-list that might  
interest you (and all):

Le 25 janv. 06 à 13:29, Eric Chatonet a écrit :

Found the following in Bugzilla with an interesting comment from Mark:

Bugzilla 2694 by xslaugh at hotmail.com (Scott Slaugh)

<snip>

     ------- Additional Comment
       #1 From
       Mark Waddingham
       2005-03-17 05:10 -------

This isn't a bug, but just a classic parsing ambiguity (it's called the
'dangling else' problem)

If-Then-Else constructs in Revolution come in several flavours:

   1) if <cond> then <conseq> else <alt>

   2) if <cond> then <conseq>
      else <alt>

   3) if <cond> then <conseq>
      else
        <alt>
      end if

   4) if <cond> then
        <conseq>
      else <alt>

   5) if <cond> then
        <conseq> else <alt>

   6) if <cond> then
        <conseq>
       else
         <alt>
       end if

   7) if <cond> then
        <conseq>
      end if

   8) if <cond> then <conseq>

If you take a look at your code, then the last command before the  
else is what
you want to be of form (8), however Revolution notices the next token  
is an
'else' followed by a newline so assumes it to be of form (6) - thus  
it expects
an extra 'end if'.

Basically, you have an 'else' dangling after an 'if' that could  
belong to either
the outer 'if' or the inner 'if'. As is standard practice in all other
languages, Revolution binds the else to the innermost 'if' statment.

You can get around this, however, by putting a new line after the  
final if
statement:
   if ... then
     if .... then ...

   else
   end if


Le 25 janv. 06 à 15:54, simplsol at aol.com a écrit :

> Chipp,
> I have the same experience with nested conditionals. I have found  
> the following format (with the "then" below the "if") helps.
>
> if tResult is "Error"
> then
>  answer "Go Ahead anyway" with "Cancel" or "OK"
>  if it is "Cancel" then exit to top
> end if
>
> There is sometimes still a problem, especially if the nested  
> conditional contains an "else" - so I always close these with an  
> "end if":
>
> if tResult is "Error"
> then
>  answer "Go Ahead anyway" with "Cancel" or "OK"
>  if it is "Cancel"
>  then
>      exit to top
>  else
>      doElseStuff
>  end if
> end if
>
> It takes more space but I find it is easier to read - and it always  
> compiles.
> Paul Looney

Best Regards from Paris,
Eric Chatonet
------------------------------------------------------------------------ 
----------------------
http://www.sosmartsoftware.com/    eric.chatonet at sosmartsoftware.com/





More information about the use-livecode mailing list