Post by bartc Post by Scott Lurndal Post by Keith Thompson Post by bartc
Leaving out 'for' wouldn't have been a great loss, as it is
hardly a proper 'for' statement with the special language
support that normally entails.
I think that's the root of your objection. You insist on a "for"
loop being similar to the "for" loop that's seen in Algol and
Pascal. It isn't. It could have been called, say, "iterate" with
exactly the same meaning.
Actually, I consider the C 'for' construct to be a natural
extension of the ALGOL and Pascal FOR constructs. They all
provide an initial condition, a terminal condition and an
optional step condition.
I can't see any connection (apart from the use of the "for"
keyword). C provides a general purpose, lower level construct
that can emulate a For statement from those other languages.
I think some historical perspective might be in order here. In
"high-level languages", the original iteration construct is DO
loops in FORTRAN. A DO loop steps an iteration control variable
through an arithmetic progression. The original FORTRAN did not
have other looping constructs IIANM - any other kind of loop
needs to be fabricated using IF and GOTO statements.
A few years after FORTRAN came Algol-60. Algol-60 also has just
one kind of looping statement in the language (ie, not counting
the if/goto kind), named 'for' rather than 'DO'. The Algol-60
'for' statement has a controlled variable, like the FORTRAN DO,
but is much richer in terms of loop control: multiple "elements"
are allowed, where each "element" may be a single value, a range
ala FORTRAN DO, or a pair of "recurring expression and 'while'
test", similar to the last two elements of a for() statement in C
(except in Algol the recurring expression is evaluated at the
start of each iteration rather than at the end).
Basic came along about two years after Algol-60, with a 'for'
statement essentially the same as DO in FORTRAN (and no other
looping constructs). It was important that 'for' be simple, both
because Basic itself was designed around being simple, and
because it ran in pretty limited hardware environments. (Later
versions of Basic added other more elaborate kinds of statements,
but they weren't there in the original Basic.)
Two or three years after Basic we see a language at the other end
of the size/complexity spectrum, PL/I. PL/I also has just one
looping construct, which like FORTRAN is named 'DO', but 'DO' in
PL/I is richer (in some ways, not all) than 'for' in Algol.
Notably, the PL/I 'DO' allows combining "while" tests with a
regular arithmetic progression, but also allows "while" tests all
by themselves, with no controlled variable at all. Shortly after
PL/I was introduced (which was 1965?), its 'DO' statement had
still more options, including an Algol-like "recurring
expression" and an 'UNTIL' form that is similar to "while" (with
the test inverted) but with the test at the end of the loop
rather than the beginning - ie, like a do...while() in C.
Several years after PL/I came Pascal, which went back to the
FORTRAN/Basic style of iteration statement, which it also calls
'for'. Pascal uses a separate 'WHILE' statement for loops with
only a test and no controlled variable, and a 'REPEAT' statement
for at-least-once style loops. Needless to say, Pascal chose
simple versions of control constructs in part because it was
intended from the outset as a teaching language.
Disclaimer: all the above from memory and/or checked against
reference material found on the net, but I have not gone back
and checked against the original references in all cases.
I expect that what one thinks of as a "traditional for loop"
depends a lot one's background. My first language was FORTRAN,
followed soon thereafter by PL/I. Somewhere in there I picked up
a little Basic but I never took Basic seriously. By the time
Pascal became the rage I was well along the path of working in
numerous languages, so Pascal wasn't ever anything special for
me. Conversely, those whose first language was Basic or Pascal
probably got used to those simpler versions of 'for' loops
(especially coupled with the 'while' in Pascal) as being the
"traditional" form of iteration construct. But the actual
history is more complicated than that. For me the limited form
of 'for' in Pascal was a special case, because I was well-versed
in PL/I by the time I first saw Pascal. I'm sure other people
have very much the opposite reaction, especially since Pascal
was much more widely learned than PL/I was. Still, before
either of those was Algol-60, whose 'for' looping construct is
decidely more elaborate than the Basic/Pascal-style 'for' loop.