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 Swift 2.0 Collections and Control Flow Control Flow With Loops For In Loops

Got stuck on for in's

Inside the body of the loop, we're going to use the multiplier to get the multiple of 6. For example, if the multiplier is 1, then the multiple is 1 times 6, which is equal to 6.

Once you have a value, append it to the results array. This way once the for loop has iterated over the entire range, the array will contain the first 10 multiples of 6.

Hello, can anybody explain me this please, my english is not the best and I cannot understand this exercise, I would appreciate a correct code too. Thank you very much!

loops.swift
// Enter your code below
var results: [Int] = []
for multiplier in 1...10 {

print("\results) times 6 is equal to (\results * 6)")

}

Also can anyone explain me what "for in" means in definition? I dont understand it and why it's called like that, I understand the "while" one and why it is called why, but I can't grasp the "for in" one. Thank you very much.

4 Answers

For in loops are, for me, the most used loops in Swift so it's imperative to get a good understanding of them. Basically a for in loop iterates over each index of an array or a given set of numbers - in this case 1 to 10. The basic structure of them look like this:

for temporaryConstant in arrayOrSet {
//do something
}

The computer will then see that and say "Ok! I am going to run the code inside of the block for each of the indexes of the array or for each of the numbers in the set, incrementing the value of the temporary constant to the index of the array (or in this case, the value of the number in the set)".

For this problem: They want us to create a for in loop that multiplies each number from 1 to 10 by 6 and append the results to an array.

var results: [Int] = []
for multiplier in 1...10 {
results.append(multiplier*6)
}
//results array now includes [6,12,18...etc.]

So walking through each part - you initially have an empty array of Integer values called "results". To fill the results we create a for in loop with the "temporary constant" called "multiplier". Then you want to add the array or indexes you are iterating through. In this case it is the numbers 1 through 10. In real applications, you generally will use an array and the temporary constant will access that value of the array instead of just numbers.

for multiplier in 1...10 {

}

We then just multiply the constant "multiplier" by 6 and append the results to the array "results".

for multiplier in 1...10 {
results.append(multiplier*6)
}

Thank you very much Ian, got it!

My last question is why it is called "for"? I just started programming and love it partly because i like to logically understand what definitions mean, like the "while" one I understand clearly, but I dont understand the "For" one.

For loops are common cross the board. "For" is basically a shorter way of saying "For each". Like, "For each of the index values, do this." Does that make sense?

Yes, thank you

illa kim
illa kim
2,283 Points

thank you!! it really helped me a lot