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

PHP

Ben Os
Ben Os
20,008 Points

Small for loop quesiton

This is the for loop, we can see the

for (int i=0; i<10; i++) { arr[i]=i; }

What is the "int" before the starter variable (i=0)?

What is the arr[i]=i in the reaction?

1 Answer

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

the int sets the loop variable i to type integer. this is necessary here because it loops through an array and array indices are integers. if we were looping through, say, words in a string, we would use the appropriate type, string. arr[i] = i means that every trip through the loop, and index i in the array is set to i, so when the loop completes the arr will be [0,1,2,3,4,5,6,7,8,9]. arrays are 0-indexed, so the first element is index 0, and we get elements with the notation arr[index], so arr[3] is index 3, or the 4th element.