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

Would this be an efficient, or even correct, way of making the FizzBuzz Generator?

// I only want to go to 50 :P

for var numbers = 1; numbers < 51; numbers++ {
  if numbers % 3 == 0 {
    println("Fizz \(numbers)")
  } else if numbers % 5 == 0 {
    println("Buzz \(numbers)")
  } else if numbers % 3 == 0 && numbers % 5 == 0 {
    println("FizzBuzz \(numbers)")
  } else {
    println(numbers)
  }
}

2 Answers

Mathieu HAYS
Mathieu HAYS
9,623 Points

Your third if will never be reached because one of its condition is already met earlier.

Ohh so should I put the FizzBuzz as the if and the rest as the else if? SO that it gets checked first?

Mathieu HAYS
Mathieu HAYS
9,623 Points

Exactly. You should always put the most specific tests first :)

Yay! Thanks a bunch man ")

Yay! :P