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

Max Briston
Max Briston
962 Points

Would someone mind someone mind showing me each line I need to write and an explanation of each one. I am having a blank

I just don't get any of it.

1 Answer

Hi Max,

For the first objective:

var results: [Int] = [] /* An empty array of Ints */

for multiplier in 1...10 {} /* A for in loop - This allows you to go through every value between 1 and 10. Each value is assigned to a temporary constant called 'multiplier' that you can then work with */

For the second objective:

var results: [Int] = []

for multiplier in 1...10 {
  let multiplesOfSix = multiplier * 6 /* The for in loop iterates over every number between 1 and 10 and multiplies it by 6. All of these values are then stored in the constant 'multiplesOfSix' */
  results.append(multiplesOfSix) /* This adds all of the multiples of 6 to the results array above. */
}

Essentially a for in loop stops you repeating yourself. In theory you could receive the same results by writing code such as:

var oneTimesSix = 1 * 6
results.append(oneTimesSix)

var twoTimesSix = 2 * 6
results.append(twoTimesSix)

var threeTimeSix = 3 * 6 
results.append(threeTimesSix)

and so on. The temporary constant 'multiplier' however just takes places of all of the individual constants that you see just above.

I hope you can understand now and see the benefits.

All the best,

Mitch