Skip to end of metadata
Go to start of metadata

Groovy supports the usual while {...} loops like Java.

for loop

The for loop in Groovy is much simpler and works with any kind of array, collection, Map etc.
Note: you can also use the standard Java / C for loop if you wish.

closures

In addition, you can use closures in place of most for loops, using each() and eachWithIndex():

  • No labels

3 Comments

  1. It seems to me that the following looping syntax should work, but the Groovy Eclipse editor v1.0.1.20070706 complains that the "def" is not valid.

    for (def number in [1,2,3]) {
        println number
    }
    

    I can lexicalize the iterator with an each(), but it seems that for() should support that too since it is a very common idiom in other languages.

  2. What happened to the do-while loop?

  3. From http://groovy.codehaus.org/Migration+From+Classic+to+JSR+syntax :
    Due to ambiguity, we've not yet added support for do .. while to Groovy