Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Foreach loop
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Groovy === [[Groovy (programming language)|Groovy]] supports ''for'' loops over collections like arrays, lists and ranges: <syntaxhighlight lang="Groovy"> def x = [1,2,3,4] for (v in x) // loop over the 4-element array x { println v } for (v in [1,2,3,4]) // loop over 4-element literal list { println v } for (v in 1..4) // loop over the range 1..4 { println v } </syntaxhighlight> Groovy also supports a C-style for loop with an array index: <syntaxhighlight lang="Groovy"> for (i = 0; i < x.size(); i++) { println x[i] } </syntaxhighlight> Collections in Groovy can also be iterated over using the ''each'' keyword and a closure. By default, the loop dummy is named ''it'' <syntaxhighlight lang="Groovy"> x.each{ println it } // print every element of the x array x.each{i-> println i} // equivalent to line above, only loop dummy explicitly named "i" </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)