Swift Logical Operators

In Swift, there are three logical operators: &&, ||, and !.

Here is a table that describes these operators:

OperatorNameDescriptionExample
&&Logical AND operator.If and only if both the operands are true, the condition is true.true && false = false
||Logical OR Operator. If either one of the two operands is true, the condition is true.true || false = true
!Logical NOT Operator.Reverses the logical state of the operand. If a condition is true, the Logical NOT makes it false.!true = false

Logical operators are used to connect truth values.

They are extremely useful in programming and you are going to use them a lot throughout your career as a software developer.

In this guide, you learn what are logical operators and logical expressions.

Let’s jump in!

Logical Operators

Logical operators are used in everyday life as we speak.

For example, take a look at this sentence: “If it is a hot and sunny day, let’s go to the beach”.

This very sentence uses a logical “and” operator to combine two logical expressions.

To see what this means, let’s re-write the sentence:

“If it is a sunny day and if it is a hot day, let’s go to the beach.”

Now, let’s split the relevant pieces of the sentence into three parts:

  1. It is a sunny day
  2. AND
  3. It is a hot day

The first part, “It is a sunny day” is a logical expression. In other words, it can be true or false.

The word “and” is a logical operator. It connects the first logical expression with a second one.

The second part of the sentence, “It is a hot day” is the second logical expression. It can also be either true or false.

Only if both of these logical expressions are true, do we go to the beach. If either one or both of them are false, we are not going to the beach.

Logical Operators in Swift

When you are talking to a computer, you cannot just say the word “and”.

Instead, you have to use a logical operator that the computer understands.

In Swift, the logical operators are:

  • && for AND
  • || for OR
  • ! for NOT

Now, let’s go back to the beach example: “If it is a hot and sunny day, let’s go to the beach”.

In Swift, you could reformulate the sentence for example as follows:

var isSunny = true
var isHot = false

var beachTime = isSunny && isHot

print(beachTime)

Output:

false

As you can see, this program can now make the decision whether or not you should go to the beach. All you need to do is specify the truth values to the variables isSunny and isHot.

Next, let’s talk more about logical operators and truth tables that help you visualize how they operate.

Logical Operators and Truth Tables

In the previous section, you saw an example of how to use the AND (&&) operator to check if two logical expressions are true.

Now, let’s further inspect the sentence: “If it is a hot and sunny day, let’s go to the beach”.

There are 4 possible scenarios:

  1. Not sunny and not hot –> no beach.
  2. Not sunny but hot –> no beach.
  3. Sunny but not hot –> no beach.
  4. Sunny and hot –> beach time.

A more formal way to represent these scenarios would be to build a truth table like this:

Is sunnyIs hotIs sunny and is hot (beach time)
falsefalsefalse
falsetruefalse
truefalsefalse
truetruetrue

This truth table considers all the possible combinations of the weather types.

In this case, there are four of them.

The rightmost column in the truth table is the result column given the two logical expressions. In our example, the truth table above shows the scenarios in which you should or should not go to the beach.

Here is how to use the truth table:

  1. Check the weather report to determine the truth values for “Is sunny” and “Is hot”.
  2. Use the truth table to find a row that matches the truth values from the weather report.
  3. The corresponding right column tells you whether it is beach time or not.

Pretty cool, isn’t it?

Now that you understand what is a truth table, let’s see the generic truth tables of the logical operators in Swift.

&& Truth Table

Given two logical expressions A and B that can be either true or false, here is the truth table for the AND (&&) operator:

ABA && B
falsefalsefalse
falsetruefalse
truefalsefalse
truetruetrue

To take home, the result of AND operator (&&) can only be true if both logical expressions are true.

|| Truth Table

Given two logical expressions A and B that can be either true or false, here is the truth table for the OR (||) operator:

ABA || B
falsefalsefalse
falsetruetrue
truefalsetrue
truetruetrue

To take home, the result of the OR operator can only be false if neither one of the logical expressions is true.

! Truth Table

Given a single logical expression A that can be either true or false, here is the truth table for the NOT (!) operator.

A!A
falsetrue
truefalse

In other words, the NOT operator (!) turns true into false and vice versa.

Last but not least, let’s take a look at some examples with the logical operators in Swift.

Examples with Logical Operators in Swift

Let’s see some helpful examples of logical expressions in Swift.

Earlier in this chapter, you already saw an example of the AND operator (&&) in the beach goers problem.

Let’s take a look at how to use the OR (||) and the NOT (!) operators.

For instance, let’s create two boolean values isStudying and isWorking, and determine whether a person is busy or not:

var isStudying = true
var isWorking = false

var isBusy = isStudying || isWorking

print(isBusy)

Output:

true

In this example, it is enough for someone to be either working or studying to be busy. The only way the person is not busy is if they are not studying and not working.

Finally, let’s also take a look at an example of using the NOT operator.

var isSunny = true

var notSunny = !isSunny

print(notSunny)

Output:

false

Here the NOT operator reversed the meaning of the boolean value isSunny. If it is true, the NOT operator turns it false and vice versa.

Conclusion

Today you learned how to use logical operators in Swift.

These logical operators are used to connect logical expressions to get a combined truth value (true or false).

For example, the sentence “If it is sunny and hot, let’s go to the beach” actually uses a logical operator to connect two logical expressions.

In Swift, logical operators can be used to combine boolean values to get a new boolean value.

In Swift, there are three logical operators:

  • &&. The AND operator
  • ||. The OR operator
  • !. The NOT operator

For example:

var isSunny = true
var isHot = true

var isBeachDay = isSunny && isHot
Scroll to Top