Re: The LOOP macro (was Re: Be afraid of XML)

RobertMaas_at_YahooGroups.Com
Date: 03/14/04


Date: Sun, 14 Mar 2004 14:11:20 -0800


> From: Barry Margolin <barmar@alum.mit.edu>
> Date: Sat, 13 Mar 2004 14:23:03 -0500
> The thing that has always bugged me about C's for loop is that the
> order of the 2nd and 3rd clauses (the end-test and the variable
> iteration) seemed backward, and there's nothing in the syntax that
> helps you to remember which is which

And one of the standard textbooks doesn't help either. See Forouzan &
Gilberg "A structured programming course using C", second edition
(2001), ISBN 0-534-37482-4, flowcharts on page 240 (with some arrows
added by me to make the flow clearer, and + at each bend in line):

Left diagram:
               |
               V
         -------------
       / | expr1 \ false
 +--->< expr3 |-------->---->+
 ^ \ | expr2 / |
 | ------------- |
 | | true |
 | V |
 | +-----------+ |
 | | statement | |
 | +-----------+ |
 | | |
 | V |
 +<-------------+ |
                             V
                +<-----------+
                |
                V
Bad things about that diagram:
(1) The three expressions aren't physically placed in the same sequence
as in source code, so it's of no use to aid memorizing which is which;
(2) The false clause seems to be coming out from the mess *before*
expr2 is evaluated, when in fact expr2 is the value tested zero/nonzero
(fake boolean) to see whether it's false or true respectively;
(3) The arrows entering the top mess at points expr1 and expr3 do show
which get executed first (initially, and after each loop,
respectively), and the arrow slightly displaced to the right from the
boundary between expr3 and expr2 does show that expr2 is last before
the statement executes, but that still doesn't show clearly that
control then passes from expr1 or expr3 to expr2 *only*, never from
expr1 to expr3 or vice versa;

Right diagram:
           |
           V
       +-------+
       | expr1 |
       +-------+
           |
           V
        /-----\ false
  +--->< expr2 >-->+
  ^ \-----/ |
  | | true |
  | V |
  | +-----------+ |
  | | statement | |
  | +-----------+ |
  | | |
  | V |
  | +-------+ |
  | | expr3 | |
  | +-------+ |
  | | |
  | V |
  +<-------+ |
                    V
           +<-------+
           |
           V

At least the flowchart makes the flow clear, and the three expressions
are shown in the correct linear sequence, but:
Bad things about that diagram:
(1) The three expressions are shown vertically, rather thna
horizontally as is usual in the syntax;
(2) There's a big gap between expr2 and expr3, which might mislead the
programmer into remembering the syntax as:
  for (expr1; expr2) statement (expr3)
Or if the syntax is remembered, it's hard to remember where in the
diagram to insert the statement.

When I was taking that class from that book, I had to invent my own
version of that diagram to remember the relation between flowchart and
C syntax:
     |
     V
  +-------+ /---------\ +-------+
  | expr1 |-->< expr2 ><--| expr3 |
  +-------+ \---------/ +-------+
             false | | true ^
                   V | |
     +<------------+ | |
     | V |
     | +-----------+ |
     | | statement | |
     | +-----------+ |
     | | |
     | V |
     | +---------->+
     |
     V
That's a bit nonstandard, having both branches of the conditional test
coming out the bottom instead of one out the bottom and one out the
side, but otherwise does that help everyone remember how the syntax
fits the flow, in case you ever are required to (yuk) write in C again?

> contrast it with PL/I and Algol's "from ... by ... to ..." syntax,
> which was adopted into LOOP

And LOOP in LISP has the added advantage that the many key words within
the LOOP-macro syntax are *not* reserved words within the entire
language as they presumably are in those other languages, in fact even
within a LOOP expression they aren't even reserved words in the sense
of affecting the parser. They only affect the semantics after the
parser is all done and EVAL or COMPILE is in control.



Relevant Pages

  • Preview chapter about the structured syntax definition of Seed7
    ... I am writing a chapter about the structured syntax definition ... will be used as base to explain the S7SSD. ... that a new statement, the 'loop' statement, should be defined. ... Priority and assoziativity ...
    (comp.compilers)
  • Re: Am I the only one who would love these extentions? - Python 3.0 proposals (long)
    ... Why is it nice enough to make it be a syntax addition, ... to point an image viewer/editor at a chunk of Python code. ... You argue consistancy to other keywords. ... Why is this new loop construct of yours useful enough ...
    (comp.lang.python)
  • Re: How about this syntactic candy?
    ... I can think of that's worse than adding a rarely needed, rarely beneficial syntax is adding one that actually allows one to lie to the compiler in a way that _breaks_ the code. ... But more significantly, IMHO, the reason that syntax has survived so many generations of this family of languages is that it's so commonly useful. ... simple for loop. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: PEP-315 ("do" loop)
    ... But it's syntax seems like an acquired taste to me. ... Would be defined to work exactly like a while loop except the test is not ... and my news client breaks the formatting. ... "improving" the basic while loop, none of which seem to be a natural ...
    (comp.lang.python)
  • Re: c++ documentation
    ... >>It doesn't bother me though I wouldn't use it in practice ... >>to declare two or more variables of different types in the loop ... > use the constructor syntax. ... syntax for UDTs than *grotesquely twist* the initialisation syntax ...
    (comp.os.linux.development.apps)