Bummer! This is just a preview. You need to be signed in with a Treehouse account to view the entire video.
Start a free Basic trial
to watch this video
Learn about the two sets of operators used in conditional statements: comparison and logical operators.
Further Reading
Exercise
Your challenge is to program a FizzBuzz generator in the Xcode Playground. If a number is divisible by 3 then you print out "Fizz". If it is divisible by 5 then you print out "Buzz". Finally, if it is divisible by both 3 and 5 then you print out "FizzBuzz". This challenge is great because you can apply everything you have learned in this course.

0:00
An if statement presents a program with a condition which needs to be satisfied.

0:06
Conditions are created using operators.

0:09
We've already seen some of these comparison operators in action.

0:13
There are two sets of operators used in conditional statements, comparison and

0:18
logical operators.

0:20
So we're gonna open up a new playground and we'll call this Comparison.

0:28
Hit Next and then hit Create.

0:30
[BLANK_AUDIO]

0:33
So I'll simply label this Comparison and get rid of that var str.

0:40
So I'm gonna first lay out all the comparison operators.

0:45
So, just gonna copy and paste these, so don't worry.

0:50
They're just coming from my other screen.

0:51
And I just wanna show you all of them in action.

0:57
So, the first one is the equal to,

0:59
where you compare two values and it tells you whether they are equal or not.

1:04
And then the second one is the not equal to, where it's comparing the two

1:09
values and letting you know if they're not equal to each other.

1:13
Then you have the greater than, where the left is greater than the right.

1:18
And you have the less than, where the left is lesser than the right.

1:23
Then you have the greater than or equal to,

1:26
which means that it also checks for the equality of the two values.

1:32
And then you have lesser than or equal to.

1:36
So the last one, as you can see, is false because 2 is not lesser than or

1:40
equal to 1.

1:42
But if we did 2, that would be true.

1:45
Or if we did 3, that would indeed be true too.

1:51
So, let's put this into action and see how it applies when programming.

1:57
Let's create a variable called distance, and we'll assign distance the value of 1.

2:05
Now, we want to write an ifelse statement to determine whether distance is near,

2:11
close, or far.

2:14
So we'll write a basic ifelse statement first, so if distance is less than 5.

2:20
So let's assume that distance here is in miles.

2:27
So I'll write a print line statement and in the print line statement,

2:33
we're going to use string interpolations, so we'll say (distance) miles is near.

2:42
So here it says, 1 miles is near.

2:45
We'll ignore that that's not grammatically correct.

2:49
So, if one mile is near, then everything else should technically be far.

2:56
So we'll write another ifelse statement or we'll write an else statement.

3:02
We'll say slash (distance) miles is far.

3:10
So now if we have a value greater than 5 or

3:14
even equal to 5, it says 5 miles is far.

3:19
Now, what if we added an elseif clause?

3:22
So let's add an elseif condition, which is greater than 5 but

3:28
lesser than something else.

3:31
So we'll say if distance is greater than 5,

3:39
then print (distance) miles is close.

3:46
[BLANK_AUDIO]

3:49
As you can see, it's showing 5 miles is far.

3:54
And technically, this should be close and it shouldn't be far.

3:59
So one way to remedy that problem is to say greater than or equal to 5.

4:05
Oh, I misspelled miles here.

4:09
There we go.

4:10
So, we're getting 5 miles is close and that's kinda what we intended,

4:16
but what if we put in a value such as, let's say 21.

4:20
It says 21 miles is close.

4:23
We can even go higher and says 121 and it still says 121 is close.

4:30
But what we want is a number between 5 and 20 to say that that

4:36
distance is close, and anything greater than 20 is far.

4:43
That's the problem in our logic over here, or in our ifelse condition.

4:49
So here is where we can use a logical operator.

4:53
So there are three kinds of logical operators.

4:56
So I'm going to write this out here, but firstly, I'm just gonna make some room.

5:00
[BLANK_AUDIO]

5:02
So, the two ampersands is the AND operator.

5:08
Then you have the two pipe symbols, which is the OR operator.

5:15
And then you have the bang, which is the NOT operator.

5:20
Now, the NOT operator we've seen when we were reviewing unary operators,

5:26
which basically negates the value, but we haven't seen the AND and the OR yet.

5:31
The AND basically expects both expressions on either side of it to be true.

5:38
So I can do if true and

5:42
true, then print out AND.

5:51
So this is simply a Boolean expression.

5:53
It's expecting a Boolean expression here.

5:56
I've written the value true, but you could actually write a Boolean expression.

6:01
So what would something like that look like?

6:03
Well, you could say if distance is greater than 5 and

6:11
distance less than 20, then print out AND.

6:17
Now, distance is neither of those.

6:19
That's why we don't see the word AND.

6:23
But let's say if we change distance to 10, now you will see the keyword AND.

6:32
Now, that's the AND operator.

6:35
With OR, you put in two pipes symbols and

6:39
if either one of these, I'm gonna change this to OR.

6:43
So if either one of these expressions is true,

6:47
it's going to execute whatever you have in the curly braces.

6:52
So, what do we mean by that?

6:54
Well, distance is greater than 5 over here, but it's not less than 20.

7:00
Let's say we change this if distance is lesser than 5 or

7:05
distance is lesser than 20, it should print out OR.

7:10
Now, the reason it printed out OR because distance is lesser than 20,

7:15
so one of the conditions is being satisfied.

7:18
And that's why it's printing out OR.

7:21
If neither of them were satisfied, so let's say if we put in 100, the OR won't

7:29
be printing out because distance is not lesser than 5 and it's not lesser than 20.

7:34
So that's how the OR

7:35
operator works, where either one of the expressions has to be true.

7:38
And with the AND

7:39
operator, both expressions on either side have to be true.

7:45
So in our case, here going back to our distance example,

7:50
we can use the AND operator and say if distance is less than 20,

7:57
then evaluate it to being close.

8:00
[BLANK_AUDIO]

8:03
So if I go back and change distance to be 19,

8:07
you will see that it says 19 miles is close.

8:11
If we put in 20, then it'll say 20 miles is far.

8:16
If we wanted to include 20, we could go ahead and

8:19
change this operator to be lesser than or equal to 20.

8:23
So this is the power of operators.

8:26
Now, what happens if we change this to be an OR statement?

8:30
Well, you will have pretty much the same effect when you use the OR statement here.

8:34
Well, one of these things have to be true, so which means that if I change this to

8:39
120, it will still tell me that (distance) miles is close, or 120 miles is close,

8:45
because it's satisfying the very first Boolean expression,

8:49
which is distance is greater than or equal to 5.

8:53
Since we don't want that,

8:54
we want only that range to be true, we're going to use the AND operator.

9:02
We've covered so much in this course and I know it can be a bit overwhelming, but

9:07
what better way to cement your learning than putting it to use.

9:11
Consider this challenge as a final exam for this course.

9:17
Your challenge is to program a Fizz Buzz generator.

9:21
It's a throwback to that old children's game.

9:24
If a number is divisible by 3, then you print out fizz.

9:29
If it is divisible by 5, then you print out buzz.

9:33
Finally, if it is divisible by both 3 and 5, the, you print out fizz buzz.

9:39
If you're confused, check out the teacher notes for more details.

9:44
This challenge is great because you can put everything you've learned in

9:47
this course into practice.

9:50
Now, don't worry if you can't figure it out because in the next video,

9:54
I will show you the answer.
You need to sign up for Treehouse in order to download course files.
Sign up