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 Basics (retired) Control Flow For-In Loop

can somebody please help ?

please help so i could get the hang of it :0

for_loops.swift

5 Answers

Hi Jurgen,

Here's how I approached that.

First, I set up an array to hold the count of 1 to 10.

let array = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

We can then use the for loop to iterate over each element inside that array. We will use that to multiply each by 7. The loop looks like:

for element in array {

}

I called my variable element - you can choose whatever works for you. The variable element will have a different value for each loop. First, it'll be 1, then on the next loop, it'll contain the value of the next member in the array, 2, and so on.

We need to do something with each value on every loop. We're putting together the 7-times table, so we need to multiply each element by 7!

for element in array {
  var result = element * 7
}

That assigns the result of multiplying the element value by 7 in another variable called result. We just now need to output this. Let's use string interpolation, right?

let array = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for element in array {
  var result = element * 7
  println("\(element) * 7 = \(result)")
}

I hope that makes sense!

Steve.

Caleb's solution is much nicer code - but somewhat lacking in explanation. ;-)

Both pass the challenge and my explanation is easily applied to Caleb's slicker code.

Steve.

ok thank you :)

Caleb Kleveter
Caleb Kleveter
Treehouse Moderator 37,862 Points

Sorry Steve, I was in a hurry; I'll try to be more descriptive next time :) .

Hey - no worries ... good work!

Steve.

Well, both answers clear the challenge so that comes down to whether jurgen meco was more concerned about passing the challenge or understanding the concepts.

I'd suspect getting past the challenge is more important!

I'll upvote you anyway and, whilst I can set best answers, I'll leave that to Jurgen! :-)

Steve.

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

This should work for you:

for times in 1...10 {
  println("\(times) * 7 = \(times*7)")
}

thanks :)

thanks :)

thanks to all, both helped me achieve one step closer to my goal of becoming a full swift developer, I'm only 16, and have a long way to go :)

you know a lot more than me even though you are 14, lucky you, I'm am going to make a cool game when i learn everything :D

Caleb Kleveter
Caleb Kleveter
Treehouse Moderator 37,862 Points

Don't worry, the hard part comes when you try to implement what you learned into your own project :) .

true, thanks :D