In my code I often find structures of the form
which suggests to me that a better, more general design for a loop would put the test between "pre" and "post" blocks:while ( 1 ) { # yadda yadda last if some_condition(); # yadda yadda }
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.loop { # begin of enclosing block # pre-test code } while ( some_condition() ) { # post-test code; }
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
In reply to Re: Control Structures
by tlm
in thread Control Structures
by artist
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |