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
Akim Ellah
1,164 PointsHow are you able to specify if the increment is positive or negative with the new school for loop?
The old school for loop had (initialization, termination, increment). This level of specification seems to be hidden here.
2 Answers
jcorum
71,830 PointsBasically you can't. In the for each loop you traverse all of the elements of an array or collection, starting with the first element and going through to the last.
If you want to go backwards you need to use the other for loop, which gives you more control over where you start, where you end, and the size and direction of the increment.
Brandon Adams
10,325 PointsYou must be talking about the "for-each" loop. for (int i : intArr[]){} It's just a shorthand method if you need to do something for each item in an array. Otherwise just use the regular for loop if you need to specify more detail in the parameters.
Akim Ellah
1,164 PointsThank you.
Akim Ellah
1,164 PointsAkim Ellah
1,164 PointsThank you.