Ideas regarding flow control and loops

BCS ao at pathlink.com
Mon Nov 5 19:29:36 PST 2007


Reply to Bruce,

> BCS Wrote:
> 
>> Bruce Adams wrote:
>> 
>>> for(int i=5;i>0;i--)
>>> {
>>> ...
>>> break;
>>> }
>>> if (i<=0) ThisNeverRuns();
>>
>> that has the same behavior but duplicates the end condition check.
>> This can be both a performance hit and can be really bad when the
>> condition changes or if it has side effects.
>> 
> The performance and side effects problems can't be avoided with your
> sugar either. Is this not what your syntactic sugar is supposed to
> alias to? If not, then what? Repeating code that changes is as always
> an issue but if its a complex condition it could (arguably should) be
> a function instead.
> 

no, the thought is that you can play games with what break does to get the 
effect.

for(int i=5;i>0;i--)
{
...
goto breakpoint; //break;
}
ThisNeverRuns();
breakpoint:;

as for performance, there is none as the break is a "jump to the end" and 
the result is a "jump to (a bit after) the end".


mixing several scope(last) and scope(break) could get fun though.

> I like the idea of "do this then" versus "do this now". Its breaks out
> of in the procedural (and even message passing OO style) where pretty
> much everything is now but I'm not entirely convinced this is the best
> way to express it. The body of a loop should have stuff to do with its
> body only. A finally block is in the right place, at the end for
> example (that's not to imply I like the earlier suggestion of adding
> finally blocks to loops either).

after thinking of all the cases I'd want, I think that there is a place here 
for something but if the structure isn't carefully considered, then it might 
do more harm than good.

> 
> Regards,
> 
> Bruce.
> 






More information about the Digitalmars-d mailing list