Bug 2304 – Add else clause to for loops - executed unless break exits
Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2008-08-22T15:12:00Z
Last change time
2015-06-09T05:14:58Z
Assigned to
nobody
Creator
d
Comments
Comment #0 by d — 2008-08-22T15:12:47Z
Python has a useful optional else clause on for loops. The else clause is executed unless the loop exits with a break.
See here: http://docs.python.org/ref/for.html
Example:
for(int i=0; i<10; i++) {
if (a[i] == b) {
printf("found at index %d\n", i);
break;
}
} else {
printf("not found\n");
}
It should be pretty trivial to implement, and is a useful feature.
Comment #1 by shro8822 — 2008-08-24T18:25:56Z
I'd prefer it be named "after" then "else" could be used if the loop never executes even once.
Comment #2 by lt.infiltrator — 2014-03-18T21:48:14Z