It seems like every programming language designed feels the need to invent a new way of saying else if. Here are the ones I've encountered:

else if
C, C++ (but not cpp!), Pascal, csh (and tcsh and *csh), most sane languages. What else did you expect?

The problem with requiring this form is in languages which expect an explicit terminator for the "else" clause; after saying "if ... else if ... else if ... else if ... else ..., you need to close 4 "else" clauses, one on top of the other. Not only is it ugly, it means you need to indent each successive clause further in. This is not a Good Thing.

cpp (more accurately, it's #elif there), sh (and zsh and bash and *[^c]sh...), Python


dvdebug says Ada also uses this horrible form. I knew Perl stole the worst features of every language. But...
... Ada?
ifelse(a1, b1, t1, a2, b2, t2, ..., ak, bk, tk, f)
M4, of course. Expands to the first tj for which aj is equal to bj, or to f if none are equal.
jclast points out that Fortran ignores whitespace. Just like you can say "GO TO" to mean "GOTO", you can say all the above (or even... "ELSE IF"...) to generate the keyword "ELSEIF".
In many languages this is done equally well with case. See pseudocode:

case $variable {
    a:   (action1)
    b:   (action2)
    c:   (action3)
    default: (default action)

This is the same thing as "if a, then action1, else if b, then action2, else if c, then action3 else default action"

As has been pointed out to me, case only works well with very simple conditional statements.

4dos .BTM batch files

You know, the usage of "else if" is not a sign of "sane" language. C doesn't implement "else if" construct, it only implements "else" construct with no required boundary markers, as you said (no, I'm not blind, although I might seem to be). This may be a Bad Thing (tm). If there are no visible boundary markers, compiler will have to use less visible boundary markers, or guess what user meant. In C(++) this means that else will include next single statement as a block. Can you even begin to imagine how many bugs this has caused (by unenlightened programmers who don't use autoindenting editors)? There is a reason perl requires braces around every construct (which is the reason perl needs specific "elsif" construct).

I'm no big fan of GNU Coding Standards, but sometimes, just sometimes, they do make sense:

if (a)
        if (b)
        else if (c)

Right? No! If you used autoindenting editor, or more importantly, sensible programming language, you'd see it's really this:

if (a)
        if (b)
        else if (c)
                do_no_a();	// do_a_no_b_or_c()

Now, I don't know how other languages you mentioned (pascal, csh, tcsh) deal with it; but I can only shudder as I imagine it. Either they do as C does, that is, guess the range, usually including next statement, line et cetera, or make construct "else if" that looks like two separate identifiers, "else" and "if", really be one construct, which could get pretty confusing (and potentially dangerous).

VHDL also uses has "elsif" variant as a keyword, but allows "else if" too. Whether they behave exactly the same may depend on which logic simulator or logic synthesiser you are using.

This should not be a surprise, as VHDL, like Ada, is a member of the Algol family of programming languages.

Log in or registerto write something here or to contact authors.