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

PLEASE HELP ME WITH "FOR-IN LOOP"

I HAVE HAD A HARD TIME DOING THE CHALENGE BY INCOPERATING THE NUMBERS OF A FOR-IN LOOP, PLEASE HELP, IM STUCK.

Sam Chaudry
Sam Chaudry
25,519 Points

Not sure where your stuck without seeing the code. But here is a simple example of a for loop in Swift

//Array which is a list of numbers var numbers = [1,2,3,4,5];

for number in numbers {

print(number);

}

//Prints 1,2,3,4,5 etc

Here you have a variable 'number' which is assigned a value from the numbers array which is then printed to the console. You can also do it in a short cut by creating a range like so

for number in 1...10 {

print(number)

}

Does the same thing- just a shorter way of doing it.

ZĂŠ Moreira
ZĂŠ Moreira
1,656 Points

Jurgen, what struggle did you have? Try to post the code here and what difficult you have.

Sam Chaudry
Sam Chaudry
25,519 Points

Ok this is tested in Swift 2.0 playground

for i in 1...10 {

let x = "\(i) * 7 = \(i * 7)";

print("\(x)\n")

}

//Logs 1 * 7 = 7

2 * 7 = 14

3 * 7 = 21

4 * 7 = 28

5 * 7 = 35

6 * 7 = 42

7 * 7 = 49

8 * 7 = 56

9 * 7 = 63

10 * 7 = 70

This works for the latest version of Swift!

1 Answer

ahmed suleiman
PLUS
ahmed suleiman
Courses Plus Student 11,685 Points

Let's say you want 6 times table from 1 to 10 , for number in 1...10{ println("(number) times 6 is (number*6)") } the (number) prints from 1 to 10 and (number*6) multiplies 1 to 10 with 6, Good luck I think you got this