Rob was demonstrating the use of a for loop to load values into an array. The i
doesn't stand for anything. It is simply a counter for the for loop. It is a standard and common practice to use a single letter, and most commonly, i
as a counter in for loops like this.
the array ages[]
has 4 elements, numbered 0 to 3. In the for loop, i is incremented from 0 to 3, so on each pass through the for loop, the i in ages[i]
will be replaced by it's value, so on the first pass, i = 0 and ages[i]
is ages[0]
. On each pass, i is incremented, so the loop will go from ages[0] to ages[1] to ages[2] and finally to ages[3].
Rob is not assigning ages[i] to GetInt(). He is doing just the opposite. GetInt() will get an integer from the user and will assign it to ages[i]. This will put an integer into each element of the array, one element on each pass of the for loop, as explained above.
ages[i] + 1 vs. i+1 :
Let's say that i = 2. Then i+1 = 3. However, ages[i]+1 will add 1 to the value stored at ages[i], and since i=2, it will add 1 to ages[2]. If ages[2] = 10 before this, then it would be 10+2 = 12 after performing the addition.
These are some of the most fundamental tools in programming. You absolutely need to understand them for every pset and for programming in general. If you don't understand these, or any other material in the class, then you should start by reviewing the lectures, the walkthroughs and the section videos until you do. Also, searching for more explanation on google or youtube will help you.
If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance.