Else clauses for loops

Magnus Lie Hetland magnus at hetland.org
Wed Apr 13 06:25:25 PDT 2011


Hi!

A feature I use surprisingly often in Python is the else clause on 
loops. Is there something similar in D? Basically, the idea is that 
your loop is "looking for something" (or some condition), and that 
you'll break out of it if you find what you're looking for. If no break 
occurs, the else clause is executed.

It makes for quite clean code in many cases, IMO. The alternative can 
be a flag variable, I guess, or a goto-statement (to right after the 
"else" bit) instead of a break. Or put it in a function, and use a 
return instead of a break ... or use scope() somehow, perhaps.

I see that the feature has been requested a few years ago [1], so there 
might not be that much demand for this sort of thing. Is there perhaps 
a D idiom here already?

[1] http://d.puremagic.com/issues/show_bug.cgi?id=2304

-- 
Magnus Lie Hetland
http://hetland.org



More information about the Digitalmars-d-learn mailing list