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

iOS Objective-C Basics (Retired) Functional Programming in C Control Flow - For Loops

Alfred Harmon
PLUS
Alfred Harmon
Courses Plus Student 1,142 Points

Going along with the video

Am i supposed to be typing down everything he types because that's what I'm doing and he knows shortcuts (that are not taught on here) and it makes it difficult to keep up. I just don't understand this whole loop thing. It's really not sticking.

2 Answers

Loops can be a bit hard to understand at first, but once you know the syntax it will make loads more sense. In a "for" loop: you have a variable that executes the loop "for" a given amount..thats where the "for" comes from. In contrast there are "if" loops that take a variable and execute "if" a value matches.

In a for loop, as an example, you could have:

for (int i =0; i < 3; i++) {
print("looping");
}

the "int i = 0" is declaring a variable called i, which will control how many times the for loop, loops. In my example, the first time this happens, the loop will see that i =0 so i is in fact less than 3, so do the command "print("looping");" , The loop then sees that its steps within the curly braces are completed and adds +1 to the value of i. Now i =1, and i is infact less than 3, so it will again execute the print("looping"); statement. You can see how the loop will continue until i =3, and i is now no longer less than 3, so the loop will exit and NOT execute its steps. This way, the statement "print("looping");" will only happen 3 times as directed.

This begins to be useful when wanting to traverse array's as done in the video. the variable, i, is used to indicate the index of an array, which he is then adding together.

Hope this helps!

I think I asked the same question when I was starting off. Do it if it helps you learn the code I suppose, sometimes you can know intuitively there's no need to type down some code you are familiar with already