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

Jason Demetillo
Jason Demetillo
428 Points

Can't figure out for-in loops

Need help trying to create the 7 times table and making it loop from 1 * 7 = 7 and 2 * 7 = 14. Very confused. I got the println to 10 lines part but I'm very confused about this in general.

Here's what i have:

for number in 1...10 { println("(1 * 7) is (number)") println("(2 * 7) is (number)") }

Nick Janes any idea? karen chiu also has the same question

for_loops.swift

2 Answers

Here's what the correct response would look like.

for number in 1...10 { 
println("\(number) * 7 is \(number * 7)") 
}

You've got to but the backslash before your parentheses. Without them, your output would look like:

  • (number) * 7 is (number * 7)
  • (number) * 7 is (number * 7)
  • (number) * 7 is (number * 7)

Adding the backslashes gives you:

  • 1 * 7 is 7
  • 2 * 7 is 14
  • 3 * 7 is 21

Also, you want the end result, the last part of your string to be number * 7. You forgot the * 7. I see you're making excellent progress though! Keep up the good work!

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