Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested `each` loops in Groovy

Tags:

groovy

Need guidance on the syntax of nested looping in groovy. How to use iterator to print values of (value of a.name, value of b.name) here?

List a
a.each {
    print(it.name)
    List b = something
    b.each {
        print(value of a.name, value of b.name)
    }
}
like image 522
user2093576 Avatar asked Jan 29 '15 05:01

user2093576


People also ask

What is the syntax for nested for each loop?

Nested loop means a loop statement inside another loop statement. That is why nested loops are also called as “loop inside loop“. Syntax for Nested Do-While loop: do{ do{ // statement of inside loop }while(condition); // statement of outer loop }while(condition);

Can you nested loops inside one another?

A nested loop is a loop within a loop, an inner loop within the body of an outer one. How this works is that the first pass of the outer loop triggers the inner loop, which executes to completion. Then the second pass of the outer loop triggers the inner loop again.

Are all nested loops o n 2?

"Are nested for-loops always O(n^2)?" To your other question, the answer is no. They aren't always O(n^2) . You can easily create a situation where one of the loops affects the iterations of the other, yielding a different complexity.

Can you nest two for loops inside another for loop?

A for loop can have more than one loop nested in it A for loop can have more than one loop nested in it.


1 Answers

List a
a.each { x ->
    println(x.name)
    List b = something
    b.each { y ->
        println(x.name + y.name)
    }
}
like image 77
tim_yates Avatar answered Oct 15 '22 19:10

tim_yates