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

stuck again

Jhoan Arango Hi sorry to bother you again i was wondering if you could help me on for in loops i don't understand them the whole concept especially the 1-10 thing an the whole println that you have to put in them is really confusing I've been stuck on them for a while like a few days . also with println when do you know how to add interplelations and wht do they help u with ?

Thank you in advance X :)

for_loops.swift

2 Answers

Hi,

here's your answer

for number in 1...10 {

println ("\(number) * 7 = \(number*7)")

}

Description: for number in 1...10 we are specifying a number 1 to 10 (in-between all numbers) then in the for in function we are using println to print a command by multiplying it by 7 --> ("(number) here it is taking the 1st number which 1 and will multiple it to 7 as following command is = (number*)")

Then as it is for in loop it will continue the cycle till number ten and then it will stop.

Jhoan Arango
Jhoan Arango
14,575 Points

Hello Alia :

You do not bother me at all, I love helping people.

When it comes to for in loops, they are my favorite. I will try to give you a good explanation on it.

  • For In β€œYou use the for-in loop to iterate over a sequence, such as ranges of numbers, items in an array, or characters in a string.”

Meaning that if you have an array, or a range of numbers you can use the for-in loop to interact with them. Say that we have an array, with a list of names. And you need to print them all out.

// Here is a list of names

var names = ["Alia ","Jhoan ","David ","Carlos ","Michael "]

// For in Loop

for name in names {
    println(name)
}

So what the loop is doing here, is going through the array, and printing each item in the array.

As you may notice, there is this name in the loop after the for. That’s a temporary constant that the loop needs so that it can place the names or items in it for a short period of time while it executes the print auction.

So it goes like this:

The loop places the first item from the array in name and then it executes whatever you indicate it to do, in this case we are asking the loop to print the value inside name, once it’s done, then the loop grabs the second item from the array and updates the constant name with it, and it executes the print action once again. This process keeps going until there are no more names to be printed.

Remember the name is a temporary constant, and you can name it whatever you like.

And for the interpolation there this great answer given by Pasan Premaratne , you can click Here to read it.

Hope you understand the for in loop better and if you still have more questions then you know how to reach me.

Good luck