History of the horrid switch statement (ii): Algol 68

The addition of an alternative branch in the case statement was provided by Algol68.

case n in
    action1,
    action2,
    action3,
    action4
out
    action5
esac

If the value of n=1, then action1 is activated, n=2, action2, and so on. If n is negative, zero or exceeds the number of actions, then the out portion is activated. For example:

int days = 
    case month in
        31,
        if year mod 4 = 0 & 
            year mod 100 /= 0 or year mod 400 =0
          then 29
          else 28
        fi,
        31,30,31,30,31,31,30,31,30,31
    out -1
    esac

This is however somewhat limiting. Pascal also provided a case statement, of the form:

case (expr) of
    L1 : S1;
    L2 : S2;
    ...
    Ln : Sn;
else
    Sm;
end;

The L1,…,Ln are case labels, which could be which could be integers, characters, boolean or enumerated data items. S1,…,Sn are statements to be executed – these can be single statements or compound statements. The else provides a default case. Here is an example:

type months = (Jan,Feb,Mar,Apr,May,Jun,
               Jul,Aug,Sep,Oct,Nov,Dec);
var currentmonth : months;

case currentmonth of
    Sep, Apr, Jun, Nov : days := 30;
    Feb : 
        begin
            if (year mod 4) = 0 and (year mod 100) <> 0
            then days := 29
            else days := 28
        end;
    Jan, Mar, May, Jul, Aug, Oct, Dec : days := 31;
else
    days := -1;
end

A workable solution which did allow multiple cases in the same branch. However all this was to change with C.

 

Advertisements

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