Programming syntax and its analogy with English (ii)

The loop constructive REPEAT means “do something again”, implying it has already happened once. The keyword FOR does not actually have a meaning that has anything to do with repetition. In programming it is all to do with setting the sequence of values for a variable, and then using them in some manner in a sequential manner. However for could just as easily be used in this context:

for x = 3.1794

But reading this implies that x is assigned the value 3.1794. There is nothing in the definition of for that would suggest it could do repetitive things, it was only really implemented as the English translation of the German word für. In this respect maybe Fortran’s use of the keyword DO was more appropriate, or even LOOP. The use of do at least implies an action. That may be what confuses the novice programmer.

do i = 1:100
loop i = 1:100

The for loop might be clearer in the following context:

for i = 1:100 do

What is clear is that the continued use of some keywords lies more in historical context than anything else. We are comfortable using for as a loop, and any other suggestions are considered  ludicrous, even though Fortran continues to use the keyword do, and Cobol the keyword perform.

A similar scenario exists with the switch statement. A switch implies making a decision, similar in concept to the railway switch, where only one track is active. It does not have the same distinctness as the case statement, as in “ case x of ”. Here the select statement of PL/I makes much more sense.

select;
    when (C1) S1;
    when (C2) S2;
    when (C3) S3;
    otherwise S4;
end;

Some of this is made more difficult by the expressive nature of the English language, stemming largely from the theft of words from other languages. For example the two words if and when. The latter was sometimes used in early languages but has disappeared from use. What is the difference between if and when? The word when is of German origin, stemming from the German wenn, meaning if.

when x > 0
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