Ad space (banner)
๐ŸฆSwift Lessons
Lesson 59 / 68

FizzBuzz (the Classic Practice Problem)

In this lesson you will solve the classic FizzBuzz problem in Swift, combining conditionals with a loop. This is for anyone searching for "Swift FizzBuzz implementation".

FizzBuzz is a staple of programming practice. You print the numbers in order, but print "Fizz" for multiples of 3, "Buzz" for multiples of 5, and "FizzBuzz" for multiples of both. It is perfect practice for conditionals.

The sample code loops from 1 to 15 with a for-in statement and checks i % 15 == 0 first, so multiples of 15 take priority, then checks for multiples of 3 and 5. The order of the checks is what makes it work.

A common stumbling block for beginners is exactly that ordering. Check for multiples of 3 and 5 first and a multiple of 15 prints only "Fizz", so you have to test the strictest condition first.

It is simple, yet it breaks if you get the order wrong, which is why it is a favourite for interviews and training. It is also ideal for practising the remainder operator (%) while cementing the basics of conditionals.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Swift directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)