Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼

Walter Bright

Dr. Dobb's Bloggers

Dangling Else, Yet Again

September 22, 2011

The so-called "dangling else" problem is a perennial one that comes up when designing a programming language. Given the code:

if (a)
if (b)

should it be parsed as:

if (a) {
  if (b)

or as:

if (a) {
  if (b)

The usual (decades-old) solution for the compiler is to associate the else with the innermost if, and the ambiguity is resolved. End of story.

But perhaps there's more to it.

It isn't ambiguous to the compiler. But it can be ambiguous to the programmer. Consider:

if (a)
  if (b)

When the indentation looks like that, clearly the programmer is intending the else to be associated with the outer if, while the compiler will silently attach it to the inner if. (At least, in a language where whitespace indenting is irrelevant, like C, C++, Java, and D. This wouldn't be an issue in Python, which regards indenting as significant.)

Now I (cough cough) would clearly never make such a heinous error (cough cough) though I am informed it does occur in the wild and causes hidden bugs. Perhaps a tweak to the language's grammar would be in order to prevent the possibility of such mistakes. This came up again in the D newsgroup.

My first thought was to simply disallow the following form:

if (a) if (b) s;

In other words, an if followed by another if without the benefit of { } would be illegal. The D newsgroup informed me how inadequate this would be. Things are a little subtler. For example, the following should be disallowed, as they are ambiguous:

if (a) if (b) s; else t;

if (a) while (b) if (c) s; else t;

And the following should be compiled without complaint, as they are not ambiguous:

if (a) if (b) s;

if (a) if (b) s; else t; else u;

if (a) do if (b) s; else t; while (c);

Additionally, the various ConditionalStatement grammars also have else clauses.

Kennytm came up with a clever parser solution that nailed it. It's a pure parser solution; no semantic analysis needed. It works by keeping track of any lexically enclosing statement that is looking for an else clause, so if an else clause is found for the current statement without a following { }, then it is marked as illegal.

As far as I know, no other language has implemented this solution, though GCC does check for a subset. I think it's great that there is still room for better solutions to ancient programming problems.


Solving the "dangling else" problem requires more than just providing a disambiguation rule for the parser. A complete solution will totally disallow code that is ambiguous, thus preventing possible bugs where the programmer thought the else was associated with one if but the parser another. In general, I find that eliminating ambiguities completely is a better, more robust programming solution than inventing an arbitrary rule to pick one.

This is implemented as a warning in the current release of the compiler. Eventually, it will be upgraded to a regular error.


I am suitably embarrassed to say that adding this to the D compiler uncovered a previously unknown bug in the D runtime library. (The debug statement has an optional else clause.)

Thanks to Andrei Alexandrescu for his helpful comments on this draft.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.