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 track - FizzBuzz Generator

I just finished doing the FizzBuzz Generator, why do we add the 0 as described on the video solution ?

Meaning I was able to visualize setting the numbers 1...20, but why do we need to put the 0 for example: if i % 3 == 0 or if i %5 == 0 ?

1 Answer

Chris Shaw
Chris Shaw
26,676 Points

Hi,

The zero is required as without it the statement can result in something other than true which is not what we're looking, for instance.

if 3 % 3 {
  println("This should never execute!")
} else {
  println("This should execute!")
}

So in the above example we're expecting a truthy value but instead because 3 % 3 equals 0 it's implied as false because 0 and false are the same thing when compared to each other.