www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 2304] New: Add else clause to for loops - executed unless break exits

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2304

           Summary: Add else clause to for loops - executed unless break
                    exits
           Product: D
           Version: unspecified
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: d strigeus.com


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.


-- 
Aug 22 2008
parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2304





------- Comment #1 from shro8822 vandals.uidaho.edu  2008-08-24 18:25 -------
I'd prefer it be named "after" then "else" could be used if the loop never
executes even once.


-- 
Aug 24 2008