Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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!