Skip to main content
15 events
when toggle format what by license comment
Feb 20 at 21:22 comment added Barmar More specifically, the programmer is likely to write the second version, and their use of indentation may think it will work as they expect, but it will actually be parsed as the first version. This is similar to what happens when they write a braceless if or else and later add a second statement without adding braces.
Feb 9 at 23:56 comment added hugomg The C specification does specify the nearest-if rule, so that all compilers will do the same thing with unbraced if-else. However, the real problem is that it's more confusing for programmers. The programmer might be thinking of the second option, but the compiler will actually do the first one.
Feb 9 at 22:34 comment added EE18 Perhaps I don't understand, but why is the dangling else problem a problem? Doesn't C unambiguously specify that it's the first of the options you gave since an else corresponds to its nearest if?
Jul 7, 2023 at 1:49 history edited hugomg CC BY-SA 4.0
deleted 13 characters in body
Jul 2, 2023 at 14:25 comment added hugomg In Python, the dangling else problem is avoided by significant indentation, which is analogous to mandating braces around the if/else body. Python's elif allows if-else-if without having to nest the elses in increasingly deeper levels of indentation (which would be analogous to the }}} problem discussed in my answer).
Jul 2, 2023 at 12:35 comment added Jack Aidley This may explain some languages, but Python uses elif and it does not suffer from the dangling else problem.
Jun 30, 2023 at 23:42 history edited hugomg CC BY-SA 4.0
added 9 characters in body
Jun 30, 2023 at 23:37 history edited hugomg CC BY-SA 4.0
added 956 characters in body
May 19, 2023 at 10:00 comment added kaya3 Also technically the dangling else problem could also be solved by only requiring braces around the if body, and not necessarily also the else body, which would allow else if to still be written. Though this might seem inconsistent to users of the language.
May 19, 2023 at 9:58 comment added kaya3 The dangling else problem doesn't contain any occurrences of else if, and would still occur in C's grammar if elseif were to be written instead. The point about block delimiters is reasonable, but I think it would be clearer if this answer said that it's a consequence of requiring delimiters around if and else blocks, which is done in part to avoid the dangling else problem but also for other reasons (hence such languages also tend to require block delimiters for loops, try/catch, and so on).
May 19, 2023 at 3:23 comment added naffetS PHP is a clike just like C and JS, and it has elseif. I wonder what the reason is there, maybe to save the keystroke.
May 17, 2023 at 3:13 history edited hugomg CC BY-SA 4.0
added 194 characters in body
May 17, 2023 at 0:16 vote accept ice1000
May 16, 2023 at 22:46 history edited hugomg CC BY-SA 4.0
deleted 76 characters in body
May 16, 2023 at 22:41 history answered hugomg CC BY-SA 4.0