Skip to main content

proposal: foreach allowing init; and ;increment terms

No replies
Joined: 2004-03-04
Points: 0

We would like to be able to write a "foreach" loop as in this example:

for ( int i=0 ; String s : listoStrings ; i++ ) { ...}

(The underlined terms represent the proposed new feature.)

Currently, you have to choose either the traditional for or the foreach syntax .
But you often want both the convenience and genericity of a foreach loop, and the indexness of the traditional for loop.

... Obviously this suggestion is meant to be general (not just for an int index variable). Any init clause and increment clause that would be allowed in a traditional for loop should be allowed in a foreach loop as in my example.

The only limitation would be that the condition (middle) term would be implicitly defined by the collection (e.g. listoStrings) . I don't think an additional condition clause based on the index variable should be mixed with the foreach collection variable. Although it could be supported with the "," syntax, an additional condition would only serve to make the implicit range of the collection irrelevant.