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 Collections and Control Flow Control Flow With Conditional Statements If Statement

If Statements - Changes in Swift 4 ?

Hey there,

So I'm following along with this video and I've entered my code exactly as shown (using X Code 9) in the video.

var temprature = 22

if temprature < 18 {
    print("It's getting chilly! I recommend wearing a light sweater.")
} else {
    print ("It feels freat outside! A t-shirt will do.")
} else if temprature < 12 {
    print("It's getting cold! Let's get that jacket out!")
}

However I'm given an error by X Code on line 6 of the code above.

"Consecutive Statements on a line must be separated by ';'. Is this an error, have I missed something, or is this a change in Swift 4 that has not yet been updated in the video?

Any help would be appreciated.

Video go's onto explain the sequence of the code and how that effects it's operation. I believe I've answered my own question / got ahead of myself a bit there.

1 Answer

Paolo Scamardella
Paolo Scamardella
24,828 Points

I'm not an iOS developer, but your else if needs to be in between and not in the end.

if temprature < 18 {
    print("It's getting chilly! I recommend wearing a light sweater.")
} else if temprature < 12 {
    print("It's getting cold! Let's get that jacket out!")
} else {
    print ("It feels freat outside! A t-shirt will do.")
}
Michael Williams
Michael Williams
Courses Plus Student 8,059 Points

Paolo's dead on.

The way I like to think of else if, is that it's a chain link (and also a condition) that allows you to connect multiple conditions (other chain links) to evaluate. On their own, the first two conditions will be evaluated, but then it gets to the else if statement at the end and is like, "Hold up, this is supposed to be in between the other two links, but it's down here doing it's own thing. What's up with that?" So just remember that else if statements always go in between the other conditions.

Thanks guy's yeah, I got a little ahead of myself in the video and found out. I appreciate your input none the less :)