Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Nesting Loops

Gendarme Docena
Gendarme Docena
1,509 Points

Nesting Loops

Can someone explain these for loop lines as an english statement please?

for (String sweet : sweetFlavors) { for (String savory : savoryFlavors) { console.printf("%s and %s %n", sweet, savory);

I think it's saying: For each sweet, connect it to each sweetFlavors. For each savory, connect it to each savorFlavors.

Because it's a for loop inside of a for loop, all we need to do is make a print statement that combines them right?

2 Answers

Hello

I would state it differently

You have 2 collections (iteratables)

Collection A has items 1,2,3 Collection B has items R, G, B

the for loop you are showing will print something like (pseudo wording)

1 R 1 G 1 B

2 R 2 G 2 B

3 R 3 G 3 B

there is connection being made between the 2 collection.

Hope this helps

Gendarme Docena
Gendarme Docena
1,509 Points

Oh okay..I think I'm understanding it.. so collection A distributes whatever it has in their list/array to collection B?