Skip to content

Variety

September 21, 2011

A bit of light observation. Different ways to do an else if with examples from languages I use or have used frequently.

Type 1: else if

Separated else and if, minimising the number of keywords required in the language. Personal favourite.

if (expr) {
    statements
} else if (expr) {
    statements
} else {
    statements
}

As in: C, Java

Type 2: elseif

Remove the space, for a bit more typing efficiency.

If expr Then
    statements
ElseIf expr Then
    statements
Else
    statements
End If

As in: Visual Basic

Type 3: elsif

Contracted by one more character, while still reading as “else if”.

IF expr THEN
    statements
ELSIF expr THEN
    statements
ELSE
    statements
END IF;

As in: Oracle PL/SQL, Perl

Type 4: elif

if expr:
    statements
elif expr:
    statements
else:
    statements

As in: Python and the Korn shell

Does anything use eif?

Advertisements
2 Comments
  1. James Bostock permalink

    I’ve never seen “eif”. Lisp does away with the keyword altogether:

    (if expr expr-if-true expr-if-false)

  2. Matt permalink

    I’ve never got around to working with Lisp, just never had the opportunity, but recently I have been getting interested in Clojure, though haven’t found an excuse to use it for anything yet.

    I was reminiscing over Sinclair Basic a few weeks ago and was reminded that it didn’t even have an else, just simply:

    IF expr THEN cmd

    I don’t remember, but I assume this led to a lot of:

    IF expr THEN cmd
    IF NOT expr THEN cmd

    It also didn’t have blocks, so you could either do it all on one line:

    IF expr THEN cmd1: cmd2: cmd3

    Or use a goto:

    If expr THEN GO TO 100

    Spaghetti.

Comments are closed.

%d bloggers like this: