Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Did you figure it out? :) If not, don't worry, here's the solution for you!
Further Reading

0:00
The most common way to solve it is using an if statement.

0:03
And this is how I did it.

0:05
Now I've wrapped all this code in a for loop, so

0:08
that we can show some code iterating through it.

0:11
But let's ignore that for now.

0:12
So this is the solution.

0:15
Now remember with an if statement, if we evaluate a certain line or

0:20
a certain compound expression to true we immediately jump into the body

0:25
of that statement and execute the code there and then we're done.

0:29
So the order in which we write our arguments matters.

0:33
So first we check for FizzBuzz.

0:35
We say that.

0:37
If the number is divisible by 3, and it's divisible by 5, we print FizzBuzz.

0:44
And the way we check that something is divisible by 3 is

0:48
using the remainder operator.

0:50
So here we're saying if i, so let's say i is 3.

0:54
3 divided by 3 has a remainder of 0.

0:57
Which means it's divisible by 3.

0:59
And so we do i % 3 == to check if the value's the same.

1:06
And we provide a 0.

1:06
And then using the and operator we create a compound expression.

1:11
So both this and this expression, that is,

1:14
divisible by 3 and divisible by 5, has to evaluate to true.

1:19
So given a number, if it's divisible by 3 and 5, we print FizzBuzz.

1:25
If not we jump to the next else if statement.

1:30
Here we're checking only for 3, we say if i is divisible by 3 and

1:35
then gives us a remainder of 0, we print "Fizz".

1:41
If not, you jump to the next one, check if it's divisible by 5 and print "Buzz".

1:46
Then the last part of the instructions was that if it's not divisible by 3 and

1:52
5 or either one, we just print the number.

1:56
I wrapped it in a for

1:58
in loop that iterates over a range of numbers from 1 to 100.

2:02
To show you all the different ways that we can go through the code.

2:05
So in the results area here, you'll see that we printed FizzBuzz six times,

2:11
which would tell you that between numbers 1 to 100,

2:15
100 included, there are only six numbers divisible by 3 and 5.

2:22
There are 27 numbers divisible by 3, 14 that are divisible by 5,

2:27
and then 53 that aren't divisible by either.

2:31
If you solved it some other way then cool.

2:34
Congrats. That's awesome.

2:37
With that, let's close this course out.

2:39
Congratulations on making it this far.

2:42
You are getting so much closer to having a proper grasp on the Swift basics.

2:47
I want to point out a couple things that I may not have mentioned before.

2:50
One, Apple has an official Swift book that you can download for free.

2:55
Everything that we have gone over so far is in the book, so

2:58
it will serve as a great refresher, especially if you need to lookup syntax.

3:02
I have included a link to this book in the teacher's notes.

3:06
Make sure you get the second version of the book, which is for Swift 2.0.

3:10
Second, all the files that I work with in any Treehouse course are available for

3:15
you as a download.

3:17
Check the download section of the video.

3:20
As a reminder, there are also teacher's notes associated with videos.

3:24
And I highly recommend you check out the resources provided.

3:28
Now that you're done, where do you go from here?

3:31
You know how we kept saying we'll talk about methods and functions later?

3:34
Well, up next is a course on Swift functions.

3:38
If you're in the track just go on to the next course.

3:41
Otherwise click on the link in the teacher's notes to go there directly.

3:44
Until the next time, happy coding.
You need to sign up for Treehouse in order to download course files.
Sign up