in reply to Control Structures

In my code I often find structures of the form

while ( 1 ) { # yadda yadda last if some_condition(); # yadda yadda }
which suggests to me that a better, more general design for a loop would put the test between "pre" and "post" blocks:
loop { # begin of enclosing block # pre-test code } while ( some_condition() ) { # post-test code; }
Both pre- and post-blocks get executed repeatedly until the test in the middle fails, at which point control passes to immediately after the post-block. The pre-block (together with the loop keyword) would be optional; omitting it results in the standard while-loop. Likewise, the post-block is optional; omitting produces the standard do-while loop.

But Perl already gives a pretty close approximation for the attractive low price of an end-of-block redo:

{ # pre-code last if some_condition(); # post-code redo; }

the lowliest monk

Replies are listed 'Best First'.
Re^2: Control Structures ;-)
by Roy Johnson (Monsignor) on May 10, 2005 at 00:49 UTC
    Or you could use the "for-do-do" loop:
    for(; do { # pre-test code some_condition() }; do { post-code }) {}
    which is really just a while-do:
    while (do { # pre-test code some_condition() }) { # post-code }
    This started out as a joke, but that construct is sensible enough not to be a joke. Bare-block-redo is still better, though.

    Caution: Contents may have been coded under pressure.
Re^2: Control Structures
by hossman (Prior) on May 10, 2005 at 07:27 UTC

    Dijkstra aparently coined this: "looping n and a half times" (or the "loop and a half" as some people have shortened it) back in 1973. Knuth mentioned it as one of the main reasons for using goto in his 1974 "Structured Programming with go to Statements" (which does not seem to be available online unless you are an ACM member) ...

    A: S; if B then goto Z fi; T; goto A; Z:

    He mentions several alternatives that he finds inferor to the goto version for various reasons, and credits Ole-JOhan Dahl as proposing a syntax he really like -- which frankly I think kicks ass, and plan on writting as a P6 macro (I think macro is the right word)...

    loop; S while !B: T; repeat;
    or in the more practical perlish way...
    loop { S; } while (!B) { T; }

      On the off-chance that this does work, the code is so tricky you probably shouldn't even think about using it in real life.

      I'm willing to accept the possibility that:

      S while (!B): T;

      is pronounced: "use T as the continue block for a while() loop that controls S." If it doesn't, then it seems like the colon should be a semicolon, and you're looping over S until B returns TRUE, then calling T.. which a loop-and-a-half doesn't do.

      I'm also willing to consider the possibility that the whole statement is somehow the conditional that controls the loop() statement, and could thus be written like so:

      loop (; S while (!B): T ; repeat) { }

      but I'm damned if I can see how the conditional in the while() loop drops through to control the loop() statement, and I have no idea why you'd want to call repeat as the loop() statement's continuation routine.

      The best way I know to express the loop-and-a-half is:

      while (1) { S; # make a calculation last if (B); # drop out when the result is right T; # adjust the parameters for another try }

      which is, at very least, easier to read. The fact that expressing the idea requires a last statement goes right to the heart of the fight that made Dijkstra's Use of Go To Considered Harmful so infamous.

      According to the key theory of structured programming (I don't reall who did the proof and don't have my references with me right now), you can write any program with nested function calls (where each function has a single entry point and a single exit point), while() loops, and if() statements. The problem is that some forms of logic are extremely ugly when written using only those tools.

      We've solved those aesthetic problems by adding things like the next, last and redo statements, continue blocks, else and elsif() statements, the capacity to exit a function from more than one place, and so on. Technically, those tools exceed the 'minimal effective system' necessary to write programs, but they don't violate the spirit of structured programming, and they make the code a heck of a lot easier to read.

        To be honest, I'm totally lost in what you are saying up to "The best way I know to express the loop-and-a-half is:" ... from there on, I'm with you 100%. and you're right, this...

        while (1) { S; last if (B); T; }

        ...is a perfectly valid way of expressing the construct in Perl(5). But I'd like something:

        • That doesn't require an explict "last" or "goto" inthe condition test
        • that has less neccessary syntactic cruft (ie: the "(1)" on the while, and the "if" in the condition test
        • forces S and T to be seperate scopes -- admitadly, this is just my own personal view on what *seems right* ... other people may not feel hat adds any value

        hence my desire to have...

        do { S; } while (!B) { T; }

        ...in perl5, which when S or T are No-Ops, breaks down to either...

        do { S; } while (!B); # OR while (!B) { T; }

        but the meaning of "do" changes in P6, hence...

        loop { S; } while (!B) { T; }
      Er, perhaps I'm badly missunderstanding you, but wouldn't:
      loop;  S  while !B:  T;  repeat;
      Translate fairly literally in to:
      { S while !B; T; redo; }
      In which case I don't understand your "practical perlish way".
        S and T should be done on every iteration, except when B goes true, in which case the final T should be skipped. Your syntax seems to do a lot of S repetitions then one T, and repeats endlessly.

        --
        [ e d @ h a l l e y . c c ]