Valhalla Legends Forums Archive | General Programming | Dangling else? --- your input

AuthorMessageTime
Yegg
[code]if (x > y)
  x--;
else
  y--;[/code]

This isn't the code used in my application, it's just for examples sake. A friend claimed this would be classified as a dangling else problem. As far as I know, and after looking it up for exact details on Wikipedia, it does not qualify as dangling else. What are your opinions?
February 13, 2009, 10:00 PM
Barabajagal
That's not a dangling else. There's no ambiguity in that statement.
February 13, 2009, 10:55 PM
Yegg
[quote author=Andy link=topic=17829.msg181634#msg181634 date=1234565731]
That's not a dangling else. There's no ambiguity in that statement.
[/quote]

Exactly. Not sure why my friend wanted to consider it dangling else regardless what I said to him.
February 13, 2009, 11:27 PM
BreW
Andy's right, you're right, your friend is just dumb and obviously doesn't know simple C syntax. That's no dangling else.
There's my input on the subject, anyway.
February 14, 2009, 4:36 AM
Yegg
[quote author=brew link=topic=17829.msg181640#msg181640 date=1234586190]
Andy's right, you're right, your friend is just dumb and obviously doesn't know simple C syntax. That's no dangling else.
There's my input on the subject, anyway.
[/quote]

My friend is actually the most intelligent coder I know. He just gets practically OCD with things like this and tries explaining them with his own sometimes unusual point of view. Usually he's correct and I'm wrong, but with this one I'm sure it's the other way around.
February 14, 2009, 5:34 AM
Barabajagal
A dangling else requires two possible if statements for the else to possibly apply to. Since your example has only one if statement, there is absolutely no question what the else belongs to. No contest.
February 14, 2009, 6:25 AM
Myndfyr
C is quite clear about the grammar of if/else.  Wikipedia uses BASIC as an illustrative demonstration of a dangling else.
February 14, 2009, 8:13 PM
Yegg
[quote author=MyndFyre[vL] link=topic=17829.msg181652#msg181652 date=1234642380]
C is quite clear about the grammar of if/else.  Wikipedia uses BASIC as an illustrative demonstration of a dangling else.
[/quote]

Thanks. I also read that page prior to posting. That's as straightforward as it gets.
February 15, 2009, 12:14 AM

Search