tags:

views:

418

answers:

8

I often see the dangling else handled as:

if (x > 0)
  if (y > 0)
    print "hello"
else
  print "world"

the parser or interpreter will actually match the else with the closest if statement, which is the "if (y > 0)".

Does any language have the pitfall of actually matching the else with the outer if or the farthest if? (except the obvious Python)

+2  A: 

Short of using space-sensitive languages (like Python, as you said), I don't see why any sensible interpretation would match the outermost if block.

Some languages prohibit this potential ambiguity by:

  1. Requiring braces for all blocks, and
  2. Providing special "else if" syntax, usually elsif, elif, or elseif.
Chris Jester-Young
+1  A: 

In Perl, braces are not optional which helps us avoid such issues:

if ($x > 0) {
    if ($y > 0) {
        print "hello"
    }
    else {
        print "world"
    }
}

versus

if ($x > 0) {
    if ($y > 0) {
        print "hello"
    }
}
else {
    print "world"
}

IIRC, most C style guidelines require/recommend braces for loops and conditionals.

Sinan Ünür
+1  A: 

C# does the "right" thing. Visual Studio's smart indent automatically puts it in the right spot too:

if (x > 0)
    if (y > 0)
     Console.WriteLine("hello");
    else
     Console.WriteLine("world");

This should hopefully be just a theoretical question, since the habit of adding curly's everywhere should be deeply ingrained in most C derivatives, otherwise you're opening up the possibility of nasty surprises.

Jeff Moser
+1  A: 

If any language did that (except for the indentation-centric languages), they would have to go into one of the "what is the worst language" lists.

This is also why I almost always use braces for conditionals, and 100% of the time when there are nested conditionals or loops. The few extra characters is worth eliminating the possibility of confusion.

Gerald
+1  A: 

In Python you cannot be ambiguous about it. Either you have

 if (x > 0):
   if (y > 0):
     print "hello"
 else:
   print "world"

or

 if (x > 0)
   if (y > 0)
     print "hello"
   else:
     print "world"

The indentation shows which "if" matches the "else". [Note: try as I might, I can't get the "else" in the first example to line up correctly under the first "if".]

In all the languages I have seen that allow this particular ambiguity, the "else" matches with the most recent "if". That may not be true of all languages that ever existed. Usually the easiest thing to do when writing the parser is to match up the "else" with the nearest "if" on the stack.

A similar question: What is the result of 5 - 2 + 1? Is it 4 or 2? Personally I always use parentheses when I write (x - y) + z or x - (y + z) because I can never remember which way the parser will go.

Mark Lutton
For the last part, IMO it wouldn't make sense for the answer to be 2. Ignoring the order of operations for a second, that would be just backwards. All the languages I've worked with go left-to-right. (Although I guess if the language was designed to be right-to-left then that would make sense, but then the programmer would/should be aware of it.)
musicfreak
There are 2 ways to interpret 5 - 2 + 1 and they both result in 41. 5 + (-2 +1) - combine the -2 and 1 first2. (5 - 2) + 1 - combine the 5 and -2 first
Hardwareguy
A: 

If it didn't match the most recent if, either the whole thing becomes nonsensical or you can't match other ifs. This (the only real alternative I can imagine) would not make sense:

if(first)
    if(second)
        if(third)
            doFirst();
        else
            doSecond();
        else
            doThird();

which is just way too wtf.

Bill K
+1  A: 

Most languages will match the else with the innermost if, and that's really the only reasonable thing for a non-whitespace-sensitive compiler to do. Many IDEs, as Jeff mentions, will format the code to match the structure, but really, using braces around all code blocks is such a sensible way to deal with this.

Michael Petrotta
+1  A: 

The Way C# works is that it matches the else statements in order of the else statements used.

ie.

if (x == 1)
    if (y == 1)
        Console.WriteLine("Hello");
    else
        Console.WriteLine("World");
else
    Console.WriteLine("All your base are belong to us.");

however if you want to change where the else goes.

if (x == 1)
{
    if (y == 1)
        Console.WriteLine("Hello World");
}
else
    Console.WriteLine("All your base are belong to us.");
zonkflut