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
David Langley
10,965 Pointsobjective C for loops
I am having a little trouble locking down my understanding of the for loop. I am having trouble with 2 parts;
- The use of i in the for loop. does it stand for anything, why is it used.
- The line sum += many [i];
I think i am very close to understanding this however i don't want to fake it and progress rather learn it properly now.
Hope you can help, so far the objective C basics is really helping.
Thanks !
3 Answers
Enrique Benitez
515 PointsWell first of all the 'i' in a For loop is just acting as a counter, to understand this, recall what it is a For loop. In simple terms, a For loop is a loop or part of code that repeats again and again only if the condition is true (Boolean), and if the condition is false, the For loop will be ignored, passed.
So, the counter just acts as a counter for the condition to be tested. For example, in this program:
for (x=1;x<=10;x++) {
NSLog@("The value of X is %i", x);
}
This For loop what does is prints out the value of x 10 times. As you can see, the value of 1 is assigned to the variable x, and then the condition is what follows, and finally, the x++ (++ is the same as x=x+1). If the condition is true, the code inside { } will execute, if not, just ignored. Remember: after each iteration (that is, after each looping), the x++ will be called, that is after a loop (iteration), so take as an example that I'm the compiler when the program runs:
A For loop, ok Assigning 1 to the variable x If x<=10 is true, I will execute NSLog@("The value of X is %i", x); and after that, the x++ is what follows, then I will return at the beginning of the line to again, do a conditional test. If not, I will ignore that code and follow the program as normal.
So, in your question, instead of 'x' I think you are referring to a 'i', that's exactly the same thing, you maybe got a program that reads:
for (x=1;x<=10;i++) {
// code here ...
}
That is, that the variable 'i' is being declared outside the for loop, it's maybe a 5, or a 2, or 1, or 7, etc. That's perfectly valid in Objective-C.
Now, as I said before, the ++ after the variable (in this case, x) is called a Post-increment operator, that is, the same as x=x+1; and is used as a shortcut for the programmer, you can also use, a post-decrement operator, that is, x--, there are also, pre-increment and pre-decrement operator, but for now, stick with the post, that is the signs '++' and '--' AFTER (as POST) the variable. The PRE-increment is (++x) and PRE-decrement (--x).
They are all the same this will print the same results:
for(int y= 10; y==10; ++y){
//code here ...
}
than:
for (int y= 10; y==10; y++){
//code here ...
}
Hope I helped you! Comment me if you have any question, see you! =)
Bill Walker
5,951 Points1) The variable i is used as an iterator. Basically, just a counter for your for loop.
for (counter, condition, update counter)
2) The line sum += many[i]; is adding the value from an array into a variable:
sum is your variable
+= is a shortcut (overloaded operator) that extends out to sum = sum + many[i];
many is an array and many[i] accesses a specific item in the array. For example, if i is 2, many[i] will access the value at position 1 (remember that arrays are zero-based in Objective-C)
Hope this helps. I'm not a very good explainer, but I try ;)
David Langley
10,965 Pointsthat makes it much more clear, thanks !
David Langley
10,965 PointsDavid Langley
10,965 Pointsfantastic help, thank you so much
Enrique Benitez
515 PointsEnrique Benitez
515 PointsGlad I helped :)