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 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.