Skip to main content
deleted 13 characters in body
Source Link
hugomg
  • 1.4k
  • 3
  • 13

They do this to avoid the dangling else problem.

In many programming languages, the syntax for else-if statements is ambiguous. For example, consider the following C program:

if(a) if (b) x++; else y++;

Which if should the else belong to? Is it the inner one?

if (a)
    if (b)
        x++;
    else
        y++;

or the outer one?

if (a)
    if (b)
        x++;
else
    y++;

This ambiguity is known as the "dangling else problem". One way that programming languages can avoid it is to require delimiters around the "then" and the "else" part of the if. Some languages, such as Swift, mandate that there must be curly braces:

if (a) {
   x++;
} else {
   y++;
}

Some languages instead use then and end keywords, for a similar effect. For example, Lua:

if a then
   x = x + 1
else
   y = y + 1
end

However, if our language requires these delimiters around the else then we cannot write else if as we do in C. Because else must be followed by an open curly brace, we would end up with open braces between the else and the if, and a long sequence of closing delimiters at the end.

if (a) {
    x++;
} else { if (b) {
    y++;
} else { if (c) {
    z++;
} else {
    w++;
}}} /*ouch!*/

Having an elseif keyword is an elegant way to avoid this }}} avalanche. We can still mandate braces after if and else, but there aren't those pesky braces between the two

Some languages such asSwift and Rust avoid the else { if problem by special casing the else if. Their grammar says that the next token after the else must be a {, unless it's an if. This is certainly a valid way to solve the problem and avoids creating a new keyword. That said, the programmer must be aware that else if is a special case. If one is used to languages with traditional if-else syntax, they might expect they are allowed to write to substitute any other statement in place of the if statement in the else if. In languages with elseif this question doesn't come up because it's loud and clear that elseif is special.

They do this to avoid the dangling else problem.

In many programming languages, the syntax for else-if statements is ambiguous. For example, consider the following C program:

if(a) if (b) x++; else y++;

Which if should the else belong to? Is it the inner one?

if (a)
    if (b)
        x++;
    else
        y++;

or the outer one?

if (a)
    if (b)
        x++;
else
    y++;

This ambiguity is known as the "dangling else problem". One way that programming languages can avoid it is to require delimiters around the "then" and the "else" part of the if. Some languages, such as Swift, mandate that there must be curly braces:

if (a) {
   x++;
} else {
   y++;
}

Some languages instead use then and end keywords, for a similar effect. For example, Lua:

if a then
   x = x + 1
else
   y = y + 1
end

However, if our language requires these delimiters around the else then we cannot write else if as we do in C. Because else must be followed by an open curly brace, we would end up with open braces between the else and the if, and a long sequence of closing delimiters at the end.

if (a) {
    x++;
} else { if (b) {
    y++;
} else { if (c) {
    z++;
} else {
    w++;
}}} /*ouch!*/

Having an elseif keyword is an elegant way to avoid this }}} avalanche. We can still mandate braces after if and else, but there aren't those pesky braces between the two

Some languages such as Rust avoid the else { if problem by special casing the else if. Their grammar says that the next token after the else must be a {, unless it's an if. This is certainly a valid way to solve the problem and avoids creating a new keyword. That said, the programmer must be aware that else if is a special case. If one is used to languages with traditional if-else syntax, they might expect they are allowed to write to substitute any other statement in place of the if statement in the else if. In languages with elseif this question doesn't come up because it's loud and clear that elseif is special.

They do this to avoid the dangling else problem.

In many programming languages, the syntax for else-if statements is ambiguous. For example, consider the following C program:

if(a) if (b) x++; else y++;

Which if should the else belong to? Is it the inner one?

if (a)
    if (b)
        x++;
    else
        y++;

or the outer one?

if (a)
    if (b)
        x++;
else
    y++;

This ambiguity is known as the "dangling else problem". One way that programming languages can avoid it is to require delimiters around the "then" and the "else" part of the if. Some languages, such as Swift, mandate that there must be curly braces:

if (a) {
   x++;
} else {
   y++;
}

Some languages instead use then and end keywords, for a similar effect. For example, Lua:

if a then
   x = x + 1
else
   y = y + 1
end

However, if our language requires these delimiters around the else then we cannot write else if as we do in C. Because else must be followed by an open curly brace, we would end up with open braces between the else and the if, and a long sequence of closing delimiters at the end.

if (a) {
    x++;
} else { if (b) {
    y++;
} else { if (c) {
    z++;
} else {
    w++;
}}} /*ouch!*/

Having an elseif keyword is an elegant way to avoid this }}} avalanche. We can still mandate braces after if and else, but there aren't those pesky braces between the two

Swift and Rust avoid the else { if problem by special casing the else if. Their grammar says that the next token after the else must be a {, unless it's an if. This is certainly a valid way to solve the problem and avoids creating a new keyword. That said, the programmer must be aware that else if is a special case. If one is used to languages with traditional if-else syntax, they might expect they are allowed to write to substitute any other statement in place of the if statement in the else if. In languages with elseif this question doesn't come up because it's loud and clear that elseif is special.

added 9 characters in body
Source Link
hugomg
  • 1.4k
  • 3
  • 13

They do this to avoid the dangling else problem.

In many programming languages, the syntax for else-if statements is ambiguous. For example, consider the following C program:

if(a) if (b) x++; else y++;

Which if should the else belong to? Is it the inner one?

if (a)
    if (b)
        x++;
    else
        z++;y++;

or the outer one?

if (a)
    if (b)
        x++;
else
    z++;y++;

This ambiguity is known as the "dangling else problem". One way that programming languages can avoid it is to require delimiters around the "then" and the "else" part of the if. Some languages, such as Swift, mandate that there must be curly braces:

if (a) {
   x++;
} else {
   y++;
}

Some languages instead use then and end keywords, for a similar effect. For example, Lua:

if a then
   x = x + 1
else
   y = y + 1
end

However, if our language requires these delimiters around the else then we cannot write else if as we do in C. We would need to insert an opening delimiter after theBecause else. We must be followed by an open curly brace, we would have opening delimitersend up with open braces between the else and the if, and a long sequence of closing delimiters at the end.

if (x < 1a) {
    print("a");x++;
} else { if (x < 2b) {
    print("b");y++;
} else { if (x < 3c) {
    print("c");z++;
} else {
    print("d");w++;
}}} /*ouch!*/

Having an elseif keyword is an elegant way to avoid this }}} avalanche. We can still mandate braces after if and else, but there aren't those pesky braces between the two

Some languages such as Rust avoid the else { if problem by special casing the else if. Their grammar says that the next token after the else must be a {, unless it's an if. This is certainly a valid way to solve the problem and avoids creating a new keyword. That said, the programmer must be aware that else if is a special case. If you'reone is used to languages with traditional if-else syntax, youthey might youexpect they are allowed to write to substitute any other statement in place of the if statement in the else if. In languages with elseif this question doesn't come up because it's loud and clear that elseif is special.

They do this to avoid the dangling else problem.

In many programming languages, the syntax for else-if statements is ambiguous. For example, consider the following C program:

if(a) if (b) x++; else y++;

Which if should the else belong to? Is it the inner one?

if (a)
    if (b)
        x++;
    else
        z++;

or the outer one?

if (a)
    if (b)
        x++;
else
    z++;

This ambiguity is known as the "dangling else problem". One way that programming languages can avoid it is to require delimiters around the "then" and the "else" part of the if. Some languages, such as Swift, mandate that there must be curly braces:

if (a) {
   x++;
} else {
   y++;
}

Some languages instead use then and end keywords, for a similar effect. For example, Lua:

if a then
   x = x + 1
else
   y = y + 1
end

However, if our language requires these delimiters around the else then we cannot write else if as we do in C. We would need to insert an opening delimiter after the else. We would have opening delimiters between the else and the if, and a long sequence of closing delimiters at the end.

if (x < 1) {
    print("a");
} else { if (x < 2) {
    print("b");
} else { if (x < 3) {
    print("c");
} else {
    print("d");
}}} /*ouch!*/

Having an elseif keyword is an elegant way to avoid this }}} avalanche. We can still mandate braces after if and else, but there aren't those pesky braces between the two

Some languages such as Rust avoid the else { if problem by special casing the else if. Their grammar says that the next token after the else must be a {, unless it's an if. This is certainly a valid way to solve the problem and avoids creating a new keyword. That said, the programmer must be aware that else if is a special case. If you're used to languages with traditional if-else syntax, you might you are allowed to write to substitute any other statement in place of the if statement in the else if. In languages with elseif this question doesn't come up because it's loud and clear that elseif is special.

They do this to avoid the dangling else problem.

In many programming languages, the syntax for else-if statements is ambiguous. For example, consider the following C program:

if(a) if (b) x++; else y++;

Which if should the else belong to? Is it the inner one?

if (a)
    if (b)
        x++;
    else
        y++;

or the outer one?

if (a)
    if (b)
        x++;
else
    y++;

This ambiguity is known as the "dangling else problem". One way that programming languages can avoid it is to require delimiters around the "then" and the "else" part of the if. Some languages, such as Swift, mandate that there must be curly braces:

if (a) {
   x++;
} else {
   y++;
}

Some languages instead use then and end keywords, for a similar effect. For example, Lua:

if a then
   x = x + 1
else
   y = y + 1
end

However, if our language requires these delimiters around the else then we cannot write else if as we do in C. Because else must be followed by an open curly brace, we would end up with open braces between the else and the if, and a long sequence of closing delimiters at the end.

if (a) {
    x++;
} else { if (b) {
    y++;
} else { if (c) {
    z++;
} else {
    w++;
}}} /*ouch!*/

Having an elseif keyword is an elegant way to avoid this }}} avalanche. We can still mandate braces after if and else, but there aren't those pesky braces between the two

Some languages such as Rust avoid the else { if problem by special casing the else if. Their grammar says that the next token after the else must be a {, unless it's an if. This is certainly a valid way to solve the problem and avoids creating a new keyword. That said, the programmer must be aware that else if is a special case. If one is used to languages with traditional if-else syntax, they might expect they are allowed to write to substitute any other statement in place of the if statement in the else if. In languages with elseif this question doesn't come up because it's loud and clear that elseif is special.

added 956 characters in body
Source Link
hugomg
  • 1.4k
  • 3
  • 13

Languages like C's haveIn many programming languages, the syntax for else-if statements is ambiguous grammars. ConsiderFor example, consider the following C program:

Which if doesshould the else belong to? Is it the inner one?

Some languages instead use then and end keywords, for a similar effect (for. For example, Lua):

However, if our language requires these delimiters around the else then we cannot write else if as we do in C. We would need to insert an opening delimiter after the else. We would have opening delimiters between the else and end up withthe if, and a long listsequence of closing delimiters at the end of the chain.

if (x < 1) {
    print("a");
} else { if (x < 2) {
    print("b");
} else { if (x < 3) {
    print("c");
} else {
    print("d");
}}} /*bleh**ouch!*/

To allow people to write pretty else-if chains, languages that require delimiters around the "else" tend to featureHaving an elseif orkeyword is an elegant way to avoid this elif}}} avalanche. We can still mandate braces after if and else, but there aren't those pesky braces between the two

That said, someSome languages special-case "else" followedsuch as Rust avoid the else { if problem by "if"special casing the else if. They sayTheir grammar says that "else"the next token after the else must either be followed by "if" or by "{"a {, but may notunless it's an if. This is certainly a valid way to solve the problem and avoids creating a new keyword. That said, the programmer must be followed byaware that else if is a special case. If you're used to languages with traditional if-else syntax, you might you are allowed to write to substitute any other kindsstatement in place of statementsthe if statement in the else if. In languages with elseif this question doesn't come up because it's loud and clear that elseif is special.

Languages like C's have ambiguous grammars. Consider the following program:

Which if does the else belong to? Is it the inner one?

Some languages instead use then and end keywords, for a similar effect (for example, Lua):

However, if our language requires these delimiters around the else then we cannot write else if as we do in C. We would need to insert an opening delimiter after the else and end up with a long list of closing delimiters at the end of the chain.

if (x < 1) {
    print("a");
} else { if (x < 2) {
    print("b");
} else { if (x < 3) {
    print("c");
} else {
    print("d");
}}} /*bleh*/

To allow people to write pretty else-if chains, languages that require delimiters around the "else" tend to feature elseif or elif.

That said, some languages special-case "else" followed by "if". They say that "else" must either be followed by "if" or by "{", but may not be followed by other kinds of statements.

In many programming languages, the syntax for else-if statements is ambiguous. For example, consider the following C program:

Which if should the else belong to? Is it the inner one?

Some languages instead use then and end keywords, for a similar effect. For example, Lua:

However, if our language requires these delimiters around the else then we cannot write else if as we do in C. We would need to insert an opening delimiter after the else. We would have opening delimiters between the else and the if, and a long sequence of closing delimiters at the end.

if (x < 1) {
    print("a");
} else { if (x < 2) {
    print("b");
} else { if (x < 3) {
    print("c");
} else {
    print("d");
}}} /*ouch!*/

Having an elseif keyword is an elegant way to avoid this }}} avalanche. We can still mandate braces after if and else, but there aren't those pesky braces between the two

Some languages such as Rust avoid the else { if problem by special casing the else if. Their grammar says that the next token after the else must be a {, unless it's an if. This is certainly a valid way to solve the problem and avoids creating a new keyword. That said, the programmer must be aware that else if is a special case. If you're used to languages with traditional if-else syntax, you might you are allowed to write to substitute any other statement in place of the if statement in the else if. In languages with elseif this question doesn't come up because it's loud and clear that elseif is special.

added 194 characters in body
Source Link
hugomg
  • 1.4k
  • 3
  • 13
Loading
deleted 76 characters in body
Source Link
hugomg
  • 1.4k
  • 3
  • 13
Loading
Source Link
hugomg
  • 1.4k
  • 3
  • 13
Loading