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 If Statement

what is wrong ?

it is work on the playground

months.swift
let months = [1, 2, 3]
var i = 0
for i in months {
 if i == 1 {
println("January")
} else if i == 2 {
    println("February")
} else {
    println("March ")

}

}

You are provided with a constant array named months that contains 3 consecutive numbers starting at 1. Using a for-in loop and an if statement, print out "January" when you encounter a 1, "February" when you encounter a 2, and finally "March" when you encounter a 3.

1 Answer

Hi zeyad.

let months = [1, 2, 3]

for i in months {
 if i == 1 {
println("January")
} else if i == 2 {
    println("February")
} else {
    println("March")
}
}

You dont need to declare a new variable for i, because the loop provides that declaration for "i" in months mean that the variable i is dynamicly declared in the loop and it loops through the months array.

and you had a space by March.

Hope this helped.

Happy Coding!!!