Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift Basics (retired) Control Flow For-In Loop

Jake N
Jake N
612 Points

in the video your not explaining how {} work...

all of a sudden it skips down lines and print pops up. hows that happen, i type it into the answer on the challenge (replacing the 2s with 7s) and it doesn't work

for_loops.swift
for number in 1...10 {

5 Answers

Stone Preston
Stone Preston
42,016 Points

the task states Use a for-in loop to print out a "7 times table". Your output should look like this:

1 * 7 = 7

2 * 7 = 14

Etc., all the way up to 10. Each line should be on a new line.

we can use a range and a for loop to loop through all the numbers 1 through 10.

we can then use string interpolation to interpolate the number variable, and the number variable * 7 to create the desired output

for number in 1...10 {
println("\(number) * 7 = \(number  * 7)")
}
Stone Preston
Stone Preston
42,016 Points

you use { } to create code blocks, or pieces of code that are grouped together. blocks of code like this are common when using loops, functions, etc

at 4:39 of this video amit opens a new block of code ( {} ) after the for in loop.

he then manually types print and selects println from autocomplete. it does not just pop up. the code challenge engine is not xcode, so it does not support code completion. you will have to type all code out yourself

Jake N
Jake N
612 Points

no, what I'm saying is it randomly goes from this line {

to down here } and he doesn't explain how you do that

Stone Preston
Stone Preston
42,016 Points

oh, you press enter/return.

for number in 1...10 {  //then press return a few times to create new lines

} // then add the brace

is that what you mean?

Jake N
Jake N
612 Points

i still can't figure out what the answer is, i feel like i am typing it in right but its still saying wrong, the error doesn't explain whats wrong about it

Jake N
Jake N
612 Points

ah okay thanks, i think i was leaving out the second \