PROPOSAL: Enhanced for each loop iteration control

Bruce Chapman brucechapman at
Wed May 6 02:45:30 PDT 2009

> I don't understand what you mean by "breaking on nested loops". Support for nested loops is why I suggested "#foo" instead of just "#"... the specification use of the loop variable indicates the nesting level. For instance, you could do something like:
> for (Thing foo : whatever) {
>     for (Thing bar : whateverElse) {
>          doSomethingWith(foo, bar, #foo, #bar);
>     }
> }
> Derek
I am not really excited about this proposal, but I just had this thought 
for a possible syntax.

The idea is to hybridize the for-each and conventional for syntax, but 
you don't need the (stopping) expression from conventional syntax 
(foreach does that), but you could include the initializing portion and 
updating portion like this


	for ( /VariableModifiers_opt  Type Identifier /: Expression ; /ForInit_/opt/ / //; /ForUpdate_/opt/ / ) /Statement

Then to do an indexed loop you could write

for (Thing foo : whatever; int i=0; i++) {
    for (Thing bar : whateverElse; int j=0; j++ ) {
         doSomethingWith(foo, bar, i, j);

but it's not that much different than what you can do (irrespective of the exotic formatting) already

int i=-1; for (Thing foo : whatever) { i++;    
     int j=-1; for (Thing bar : whateverElse ) { j++;
         doSomethingWith(foo, bar, i, j);


More information about the coin-dev mailing list