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

how is this wrong?

let months = [1, 2, 3] for month in months { if(month == 1){ println("January") } else if(month == 2){ println("February") } else if(month == 3){ println("March") } }

5 Answers

David Galindo
PLUS
David Galindo
Courses Plus Student 2,294 Points

for the if statement challenge all you need to do is the for-in with the if , else if and else like so.

for month in months { if month == 1 { println ("January") } else if month == 2 { println ("February") } else { println ("March") } } Always make sure you have the same number of open and closed curly braces.

obey me
obey me
1,135 Points

for the last condition it should only else not else if because it's the last condition which means it neither ==1 and == 2

     let months = [1,2,3]
        for month in months{
         if (month == 1) {
            println("January")

             }
              else if (month == 2)

                   {

              println("February")

                }
          else{

              println("March")
           }



               }

// hope it helps please vote this answer

But if more elements are added to the array, say 4,5,6 it should not print March, thus I had it in an if else statement to future proof the desired functionality while abiding to the instructions. Does SWIFT "require" an else statement in an if/else ? If it does not then I would believe the quiz is wrong.

obey me
obey me
1,135 Points

I get what you're saying but remember how the [if, else if ,else ] statements work . when you said if the condition could true or false so if it is true execute this code if not return what i have in the else :when it comes to two conditions.

when you have more condition ,for example " a million conditions" the last one should be an else because every case before that case is not true so the last condition is true .. that is it i Hope this help.

Francisco Navarro
Francisco Navarro
14,185 Points

Swift doesn't require you the else statement, if it isn't necessary. In cases like this (enumeration) I highly recommend you to use the "switch" statement as it is clearer.

For example:

let months = [1,2,3]

for month in months{

       switch(month){

           case 1:

                println("January")

          case 2:
               println("February")

          case 3:
              println("March")

}

I totally agree, but the exercise during one of the modules specifically said to use if/else statements.

obey me
obey me
1,135 Points

It does how many condition you the last one should always else because every case before the last did not work therefore the last it is a must

Francisco Navarro
Francisco Navarro
14,185 Points

sorry but I don't understand what you mean, what is your doubt?