Repeated
action syntax (loops or recursion)
While loops
While loops in Groovy are executed in the manner they are in Java. The condition in the while loop is a boolean value that if true, makes the functions inside the while loop to execute. If the boolean is changed to false then the while loop is then terminated.
For Loops
Similar to the syntax Java uses for for loops, groovy starts the for loop statement with a variable declaration, an expression and then incrementation.
For In Loops
This is not a normal for loop. Instead, a for in loop takes a variable and then checks to see if the variable is in a certain range. For example if there was an array of numbers such as int [] arrary =[0,1,2,3,4,5,6,7,8,9], a for in loop could take a value, and check to iterate all of the given values that are inside the array, the loop would be set up in the way shown below.
for ( int i in array)
{
println "${i}"
}
So then the output for these lines of code would be : 0,1,2,3,4,5,6,7,8,9. Along with the for in loop iterating through arrays, the for in loop can also iterate through a range if needed. An example of this is shown below.
for (int i in 1..9)
{
println"${i}"
}
The output would then be an iteration of the range of the given values.
While loops
While loops in Groovy are executed in the manner they are in Java. The condition in the while loop is a boolean value that if true, makes the functions inside the while loop to execute. If the boolean is changed to false then the while loop is then terminated.
For Loops
Similar to the syntax Java uses for for loops, groovy starts the for loop statement with a variable declaration, an expression and then incrementation.
For In Loops
This is not a normal for loop. Instead, a for in loop takes a variable and then checks to see if the variable is in a certain range. For example if there was an array of numbers such as int [] arrary =[0,1,2,3,4,5,6,7,8,9], a for in loop could take a value, and check to iterate all of the given values that are inside the array, the loop would be set up in the way shown below.
for ( int i in array)
{
println "${i}"
}
So then the output for these lines of code would be : 0,1,2,3,4,5,6,7,8,9. Along with the for in loop iterating through arrays, the for in loop can also iterate through a range if needed. An example of this is shown below.
for (int i in 1..9)
{
println"${i}"
}
The output would then be an iteration of the range of the given values.
In the For In range example that you provided:
ReplyDeletefor (int i in 1..9)
{
println"${i}"
}
Is there somewhere in this loop that the array being looped through goes? It looks like the program should know to iterate through the range 1-9 but how does it know which array it is iterating through?