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

Problem understanding Loops I have been following the courses but cant seem to grasp the concept of what loops are.

I don't understand what the for in loop does, and same thing goes for the while and repeat while loop. I have watched the course videos but still don't get it. I would appreciate if someone could explain the 3 loops in a simple way. Thank you

2 Answers

Loops (at least the while loops) are actually pretty simple, all they do is run a block of code over and over again until a certain condition evaluates to false.

Loops are easiest to illustrate with code examples:

var timesLooped = 0

/* The thing directly following the while statement is the condition for running the loop
 for this loop the condition is "timesLooped < 5"
 or put more simply run while timesLooped is set to a number lesser than 5 */
while timesLooped < 5 {
    print("This will be printed 5 times")
    timesLooped = timesLooped + 1
}

Since the timesLooped variable is increased by one each time through the loop, and it starts at 0, the loop will run 5 times before the condition of timesLooped < 5 is false, at which point the loop ends.

If we don't increase the timesLooped variable inside the while loop then the loop would run endlessly.

A repeat while loop works almost identically, the difference is that if a while loop's condition is false before the loop starts then the loop never runs, whereas a repeat while loop runs at least once even if the while condition it is attached to is false from the start, example:

var timesLooped = 10

/* while statement is never run*/
while timesLooped < 5 {
    print("This will be printed 0 times")
    timesLooped = timesLooped + 1
}

/* repeat statement is run once */
repeat {
    print("This will be printed 1 time")
    timesLooped = timesLooped + 1
} while timesLooped < 5

A for in loop works somewhat differently from a while loop, it is used to run a block of code on each item in a list or range, let's say you had a list of names and wanted to run a block of code on each of those names, the easiest way to do that would be with a for in loop as illustrated here:

var nameList = ["Tom", "Andy", "Peter", "Mona", "Lisa", "Elizabeth"]

for name in nameList {
    print("hello there " + name)
}

In a for in loop each item in the list is assigned to a variable inside of the loop, in the example above I named that variable "name".

The first time though the loop the first item of nameList (Tom) will be assigned to name, the second time though the loop the second item (Andy) will be assigned to name, and so on.

So the result of running the above code would be:

Hello there Tom
Hello there Andy
Hello there Peter
Hello there Mona
Hello there Lisa
Hello there Elizabeth

I tried to make those examples simple, but if you found them unclear or are confused about something then feel free to ask me follow up questions, I'll do my best to clarify anything you are confused about.

i have been trying to figure these out ive re watched the videos a 10000 times and could never understand i dont understand why they make it complicated it is suposed to be explained easily as u did ! thank you!!!!!!!!!!!!

Thank you andren!