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 trialDonte nall
1,961 Pointsfor in loop and print
I just started to learn about "for in loop"and print . i am a bit confused.
Could someone give me a better example/better explanation of a for in loop and print in a Array type? not sure if i understand the concept very well.
2 Answers
Ben Masel
2,004 PointsI am not sure if this will help you or if it is completely right but i think about for in loops as in: "for" is creating the loop, then you create a name for the loop, "in" also declares a for in loop, then the condition. So if i had a variable called "a" that kept changing and every time it equaled 1 it would do something i would do: for randomName in a == 0 { print("randomString") } then you open the bracket put a statement of code then close the bracket. in a print statement you can print the loop so instead i would do: print(randomName) you can also do floats, doubles, Integers, variables and constants. { There are probably a few more : ) }
Hope this helps!
Donte nall
1,961 PointsThanks Benjamin, this helps . I have another question if you don't mind.
This is a challenge task : for in loop For this challenge, we're going to use a for in loop to compute the multiplication table for 6 and append the results to an array.
To start off, I've created an empty array of type Int and assigned it to a variable named results. Note that we have to explicitly declare the type here so that the compiler knows this array will hold Int values.
Your task for this step is to create a for in loop that iterates over a range. Multiplication tables typically range from 1 to 10 (with 10 included) so the range you are iterating over should go from 1 to 10.
For in loops also define a constant that temporarily stores the value in the iteration process. For the loop you're writing, name this constant multiplier.
Why is this Array empty to answer this question. I end up figuring out the answer but i don't understand why the array is left blank to create a for in loop for "multiplier" of 6 using a closed range operator.
var results: [Int] = [] for multiplier in 1...10 { result.append(multiplier * 6) }
Stanley Odestin
2,246 PointsDonte, this is the answer to your last question regarding "Why the array is left blank to create a for in loop for "multiplier" of 6 using a closed range operator?"
The for in loop is not printing anything but returning the value of 1*6 to the constant created earlier var results: [int] = []. So if you were print the constant results with a line of code, the print screen would have shown the value; which is 6.
Hope that helps clarify things a bit!
Ben Masel
2,004 PointsBen Masel
2,004 PointsSorry if i have not explained the concept very well : (