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 Java Objects Creating the MVP Current Progress

lidor nir
PLUS
lidor nir
Courses Plus Student 1,101 Points

What is going on in that loop ?

Can someone explain this loop? is he declaring a char at the for statement? whats is going on ? how can you use ":" and what does it mean, did i skip a few videos or something?

Noam Levy
Noam Levy
4,480 Points

this is the "enhanced for loop" and it is used when you need to go through all the instances in a variable, when you can't tell how many times the loop should run or when you dont need an index for the times the loop ran. so as the video shows:

for (char letter : example.toCharArray()) {
System.out.println("We've got the letter " + letter);
 }
/* this enhanced for loop means - for every instance in (which replaced by ':') the example array 
(that you turn to array of characters)
 store the instance in the new char variable "letter" and print the line + what's stored in the "letter" variable*/

1 Answer

lidor nir
PLUS
lidor nir
Courses Plus Student 1,101 Points

yeah he answered it later in another course and it's all clear now. Thank you very much for your help!