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 Counting Scrabble Tiles

Pedro Araya
Pedro Araya
1,912 Points

What is a "foreach" loop?

I have a doubt. what means this ":" on a for loop?

       for (char tile : tiles.toCharArray()) { // Here are the two dots, but I'm confused...
        if (tile == letter) {
            counter++;
        }

1 Answer

Chris Sederqvist
Chris Sederqvist
7,596 Points

It is syntax for saying for each char (tile) in tiles (converted to char array) increment counter if the tile (char) is equal to letter.

the ':' means for each of the type of item on left side contained in the structure on the right side, so to speak... (for tile in tiles)

for (char tile : tiles.toCharArray()) 
    if (tile == letter) {
        counter++;
}