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():
Labels
3 Comments
Hide/Show CommentsAug 09, 2007
Chris Dolan
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.
Jul 02, 2008
Travis Wellman
What happened to the do-while loop?
Nov 29, 2009
Reiner Saddey
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