This course will be retired on May 31, 2020.
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
In Swift, logic is represented by the Boolean values true and false. These boolean values are what allows us to use if statements to execute code based on certain conditions. But sometimes a single check is not enough. We might want to execute a certain block of code if more than one condition is satisfied and for this we use logical operators.

0:00
In Swift, logic is represented by the Boolean values true and false.

0:05
These Boolean values are what allows us to use IF statements

0:08
to execute code based on certain conditions.

0:11
But sometimes, a single check is not enough.

0:15
We might want to execute a certain block of code

0:17
if more than one condition is satisfied.

0:20
Swift comes with three logical operators that we can use to achieve this task.

0:25
Let's start with the AND operator.

0:27
Let's assume we have a logical condition a.

0:32
Assume we also have another logical condition b.

0:35
The logical AND operator, denoted by a double ampersand,

0:40
creates a single logical expression where both a and

0:44
b need to evaluate to true for the entire expression to evaluate as true.

0:51
Let's replace a with a real logical expression.

0:55
3 greater than 2, for example, and b with another.

0:59
Now, both these expressions evaluate to true.

1:02
So, the entire statement is true.

1:05
However, with the AND

1:06
operator, if any one of them is false, the entire expression is false.

1:12
Let's look at an example in code.

1:14
So, as always, we will put a comment in here and say, a Logical Operators.

1:22
Logical operators are mostly used with IF statements, so, let's write one.

1:27
What if the temperature is above 7 degrees but below 12?

1:31
It's cold enough for a jacket, but not cold enough that we need gloves.

1:35
It's great scarf weather though.

1:38
So, I can write, if temperature is greater than 7 and,

1:44
double ampersands, temperature is

1:48
less than 12 then we'll print("Might

1:53
want to wear a scarf with that jacket").

2:00
Right now, the temperature is 9 degrees.

2:03
This means that the first logical condition evaluates to true,

2:07
temperature is greater than 7.

2:09
And the second logical condition evaluates to true as well, 9 is less than 12.

2:15
So, this entire expression evaluates to true.

2:18
If I go back up, however ,and change it to 14,

2:21
we'll see that the print statement regarding the scarf disappears.

2:26
This is because even though the first statement evaluates to true,

2:29
the second temperature less than 12 evaluates to false.

2:33
Therefore, the whole expression is false.

2:36
Let's add some pretend variables to our example.

2:40
So, down here, we'll say var isRaining

2:45
= true, var isSnowing = false.

2:50
Here, we have 2 Boolean values that tell us if it's raining or if it's snowing.

2:55
In either one of these conditions, my regular shoes would get soaking wet.

2:59
So, the app should tell me to wear boots.

3:02
We could do that by performing 2 checks.

3:05
First, to see if it's raining, then, to see if it's snowing.

3:08
But that's unnecessary extra code.

3:11
To do that, we can use another logical operator, the OR operator.

3:16
The OR operator, just like the AND

3:18
operator, works on 2 logical expressions a and b.

3:23
In this case, however, only one of them has to be true for

3:26
the expression to be evaluated as true.

3:29
The OR operator is denoted by a set of vertical lines.

3:33
These lines are commonly called piped characters.

3:37
So, more formally, the OR

3:39
operator is an infix operator made of 2 adjacent pipe characters.

3:44
Let's add an IF statement to check if we need a rain boots.

3:48
So, let's say, if isRaining or isSnowing.

3:55
Then, we'll print out ("Get out those rain boots!").

4:01
Or we'll just say boots.

4:04
You'll notice that I'm simply listing the variables as the expression, right?

4:08
Over here, typically, we put in a logical expression.

4:12
Now, because these are Boolean variables, we can use them directly as an expression.

4:18
Over here, a temperature greater or less than 7 has to evaluate or

4:23
has to resolve to either true or false.

4:26
And since our isRaining and isSnowing values are already true or

4:29
false, we can evaluate them directly.

4:31
We can use them as expressions.

4:34
So, here, we're saying, if it's raining or if it's snowing, get out those boots.

4:39
Both these expressions AND and OR come in handy quite often.

4:44
Now, I said we have a third logical operator, but we've already seen that one.

4:47
So, let's just refresh our memories.

4:50
The third operator is the NOT operator.

4:54
The NOT operator is a prefix operator,

4:57
then is it appears before its target and is denoted with an exclamation mark.

5:03
The NOT operator makes a true statement false and vice versa.

5:08
When you see an exclamation mark in a logical test, you should read it as not.

5:14
So, for example, I can write if exclamation point is raining.

5:18
And then, open the body of the IF statement.

5:23
You should read this as if it is not raining, then do something.

5:28
So, here, we'll say if it's not raining, print( "Yay the sun is out" ).

5:36
Now, over here isRaining = true.

5:39
Now, the NOT operator takes something and flips it around.

5:43
True becomes false, false becomes true.

5:45
So, we're saying, if it is raining,

5:48
if it's true, then flip it and it becomes false.

5:52
At that point, this entire expression evaluates to false, so,

5:57
we do not go into the body of the IF statement.

6:01
Now, if isRaining is false, then, when we flip it using the NOT operator,

6:06
the entire expression becomes true.

6:09
And then, we step into the body of the IF statement.

6:12
So, right now, because isRaining is true,

6:14
we don't see Yay the sun is out in the print statement list.

6:19
However, if we were to change this to false, you will see it immediately.

6:23
And that wraps up our discussion on logical operators.

6:26
There are 2 things you should probably know, though.

6:29
Both the AND and OR operators work through what's called short circuit evaluation.

6:35
These operators are lazy and would like to do the least amount of work possible.

6:40
With the AND operator, both conditions need to be true for

6:44
the entire expression to be evaluated as true.

6:48
If the first expression is false, then, there's no point

6:51
checking the second expression because one false means the whole thing is false.

6:56
This is exactly how the compiler works.

6:58
If it encounters a false in an AND

7:00
expression as the first one, it won't even check the second expression.

7:05
Similarly, with an OR expression, if the first expression is true, the compiler

7:10
won't bother to check the second one because one true statement is enough for

7:15
the entire expression to return true.

7:18
Logical operators can be chained.

7:20
So, it's not necessary to only evaluate 2 conditions.

7:25
For example, I can check that 3 conditions are all true with 2 statements like this.

7:32
So, I can say, if isRaining and isSnowing.

7:38
And something like temperature less than 2, then,

7:43
I can say, print("Put some gloves on!").

7:48
Now, even cooler, although, depending on how you use it, it may not be readable.

7:53
We can use parentheses to create compound expressions.

7:56
So, I'll say if is, or let me open up the parentheses.

8:01
And in here, I'm going to put the first logical expression.

8:05
Now, this expression is going to be a set of 2 expressions.

8:08
So, I'll say, if (isRaining II isSnowing) as our first expression.

8:15
And temperature greater than 2, then, print("Definitely,

8:21
get the leather gloves out!").

8:27
Now, in this case, the AND operator sees 2 expressions.

8:32
One on its left and one on its right.

8:35
Now, the one on its left is itself a series of 2 expressions that we're going

8:40
to resolve down to one using the OR operator.

8:44
So, remember, with an OR operator, if either expression is true, then,

8:48
the entire expression is true.

8:49
So, over here, isRaining is true,

8:52
and because this is a short circuit operator, this is true.

8:57
We don't even bother to check the second one, so, this entire expression is true.

9:01
So, now, over here, we have a true for this side, and

9:05
then, we have a second expression here.

9:07
So, for this to print out, the temperature has to be less than 2 because an AND

9:12
operator requires 2 true values on either side.

9:16
Compound expressions are rare though and lead to some complex logic, and

9:19
make things unreadable.

9:21
So, it's best to keep it simple.

9:24
All right, now that we're familiar with logical operators,

9:27
let's test our knowledge with a quiz.
You need to sign up for Treehouse in order to download course files.
Sign up