Language design: the control structure “terminator”

Languages have evolved in one of two streams: those that incorporate the use of an end-marker, or structure terminator, and those that don’t. Those that don’t use an end-marker, classically fall into the trap of syntactic issues such as the dangling-else. Those that do, are often better structured, relying less on the specific use of block statements to enclose groups of statements. Many languages from the ALGOL family (Modula, Modula-2, Oberon, Ada), require end-markers on all structured statements. Modula-2 uses end to terminate all its structures, and both Fortran 90+ and Ada use the end if clause. Algol68 decided on using the initial keyword backwards, for example if ends in fi, case ends in esac.

Let’s consider Ada for example.

if condition then
    sequence_of_statements
else
    sequence_of_statements
end if;

The if and end if structure avoids dangling-else ambiguity. Now writing a nested series of if statements looks like this:

if condition1 then
    S1
else if condition2 then
         S2
     else if condition3 then
              S3
          else
              S4
          end if;
     end if;
end if;

Of course this code can be made even simpler with the use of an else-if combination clause. In the case of Ada, this is elsif.

if condition1 then
    S1
elsif condition2 then
    S2
elsif condition3 then
    S3
else
    S4
end if;

More recently, the language Julia incorporates the use of the end keyword to terminate both control structures, and functions.

Advertisements

One thought on “Language design: the control structure “terminator”

  1. codeinfig says:

    “Modula-2 uses end to terminate all its structures”

    thanks so much for this! i didnt know if there were more languages that had a universal block terminator– other than unindenting in python.

    in the 60s and 70s it seems like it was common to have an algol-style reversed command name (“fi”, “esac”, etc.)

    bash, which i like despite this, throws consistency to the wind using “fi” with if, “done” with for, “function” with braces AND a required semicolon… it has something for everybody?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s