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 Comparison and Logical Operators

Why did he say @ 3:57 "technically this should be close"?

var distance = 5

if distance < 5 {
    println("\(distance) miles is near")
} else if distance > 5 {
    println("\(distance) miles is close") /*Amit said here that it should say "close" in the output but why should it? Distance is not greater than 5. */

} else {
    println("\(distance) miles is far")
}

Sorry, I don't know why the code came out like that.

J.D. Sandifer
J.D. Sandifer
18,813 Points

Amit was referencing the "5 miles is far" output visible on the right in the console output section of Xcode. 5 miles is close, not far (according to the definitions he used for that demo).

He goes on to fix the code by inserting a >= operator in the else if comparison, thus fixing the code and displaying "5 miles is close" in the console.

I realize that. I saw the output on the right. But he first declared "var distance = 5". His else statement says "else if distance > 5 println("(distance) miles is close". Distance is NOT greater than 5 (according to his declaration) so it seems like it SHOULD output "5 miles is far".

Anywhoo, I'll review it again. Cheers

J.D. Sandifer
J.D. Sandifer
18,813 Points

Yeah, the code worked the way we would expect it to work. I don't think he should have said "technically" - it is a little confusing.

He probably should have said something like, "Ok, but we don't want it to display 5 miles is far, we want it to display 5 miles is close. So let's change the code to make that happen.")

1 Answer

J.D. Sandifer
J.D. Sandifer
18,813 Points

Amit was referencing the "5 miles is far" output visible on the right in the console output section of Xcode. 5 miles is close, not far (according to the definitions he used for that demo).

He goes on to fix the code by inserting a >= operator in the else if comparison, thus fixing the code and displaying "5 miles is close" in the console.