Reputation: 187499
I'm pretty sure the following is legal in Java
for (int var1 = 2, var2 = 3; var1 < 10; ++var1) {
System.out.println(var1);
}
But when I try to run it in the Groovy console, I get this error
unexpected token: =
Are multiple variable declarations unsupported by Groovy or is there another reason why this isn't allowed?
Upvotes: 6
Views: 2761
Reputation: 6045
It's a common gotcha for Java Developers. See this link for more detail:
Common gotchas you can use only one count variable.
Excerpts from the link:
for Loops
Another small difference is that you can’t initialize more than one variable in the first part of a for loop, so this is invalid:
for (int count = someCalculation(), i = 0; i < count; i++) { ... }
and you’ll need to initialize the count variable outside the loop (a rare case where Groovy is more verbose than Java!):
int count = someCalculation() for (int i = 0; i < count; i++) { ... }
or you could just skip the whole for loop and use times:
someCalculation().times { ... }
Upvotes: 8