Loop iterator - example.txt

Rick C. Hodgin Rick_member at pathlink.com
Sun Apr 30 20:45:05 PDT 2006


In article <op.s8tl32d56b8z09 at ginger.vic.bigpond.net.au>, Derek Parnell says...
>
>> Anyways, easy to add keyword, but in the same place as "continue", so  
>> why do it?
>
>Because 'continue' and 'retry' are not the same thing. 'continue' means go  
>to the next iteration, and 'retry' means repeat the same iteration.
>
>....CONCEPTUAL CODE....
>
>    for_start:
>        init_code;
>    for_test:
>        if ( end_condition ) goto for_end;
>    for_body:
>        do_something;
>        if <A> goto for_index; // continue
>        if <B> goto for_test; // retry
>        if <C> goto for_start; // restart
>        if <D> goto for_end; // break;
>    for_index:
>        update_index;
>        goto for_test;
>    for_end:

Derek, you nailed it here.  These should be the exact keywords used for this
concept, because these are 
the sum total of mechanisms which could be applied.  And they have the added
benefit of 
accomplishing the same things that can be accomplished in other ways, but in a
more straight-forward 
and elegent manner.  I applaud your contribution.

One other potentially desirable condition that I could've used a few times is
this:

>....CONCEPTUAL CODE....
>
>    for_start:
>        init_code;
>    for_test:
>        if ( end_condition ) goto for_end;
>    for_body:
>        do_something;
>        if <A> goto for_index; // continue
>        if <B> goto for_test; // retry
>        if <C> goto for_start; // restart
>        if <D> goto for_end; // break;
>        if <E> goto for_invalidate_end; // ibreak
>    for_index:
>        update_index;
>        goto for_test;
>    for_invalidate_end:
>        set test value to some value indicating hard break
>    for_end:

I propose the actual syntax for this invalidate-break to be:
for (i=0; i<10; i++)
{
// Code goes here for loop
} (i = -1);

With the invalidated value being specified at the end of the for loop as the "(i
= -1)" code.  This would 
allow instances where the for loop was exited through a hard ibreak command to
set the value to some 
identifiable condition in an elegant manner.  I further propose that the block
of code contained there 
could also be either a series of semicolon delimited commands (which would
execute multiple 
instructions) or a block of code enclosed by braces within the trailing
parenthesis, as in:

for (i=0; i<10; i++)
{
// Code goes here for loop
} ({
// Invalidation code would go here
});

These are some thoughts I've had to make the elegence of the programming
concepts more straight-
forward, to be supported by the compiler, and to remove the need for certain
types of work-arounds 
right now for (perhaps even not so common) bits of code (but rather code that
needs to work in this 
way, and to do so in a straight-forward manner).

- Rick C. Hodgin





More information about the Digitalmars-d mailing list