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 (Retired) Creating the MVP Current Progress

Joel Lithgow
Joel Lithgow
7,414 Points

Use of the "for" loop in this video doesn't make sense to me.

Im not sure if its bad explaining or my interpretation of this but could someone explain the for loop in this video as Craig makes around 4:30. I am use to knowing for loops as taking 3 inputs such as:

for (int i = 0; i<10; i++) {
  System.out.printf(i);
}

1 Answer

Nico Julian
Nico Julian
23,657 Points

Hi there,

The type of for-loop that Craig is using around 4:30 is called a for-each, or an enhanced for-loop. This type of loop will iterate over each item in an array, rather than using a counter variable like a regular for-loop. The one shown in the video will take mAnswer, split it into individual characters, then go over each letter in it. The char, letter, can then be used inside the loop.

for (char letter: mAnswer.toCharArray()) {
}
Joel Lithgow
Joel Lithgow
7,414 Points

thank you for explaining, I looked this up earlier and found a great youtuber who goes by the name of "theNewBoston", his video helped me a lot and explained it very well. https://www.youtube.com/watch?v=w41D0V-BnKQ