In Swift, there are three logical operators: &&, ||, and !.
Here is a table that describes these operators:
Operator | Name | Description | Example |
---|---|---|---|
&& | 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 connecting 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.
In the very next chapter, you are going to use them, so make sure to understand how they work and why they are useful.
This chapter is part of a completely free Swift for Beginners guide. Make sure you have completed all the chapters from the start before reading this one.
Disclaimer: To learn Swift, you have to repeat everything you see in this guide and in the following guides. If you only read the guides, you are not going to learn anything!
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:
- It is a sunny day
- AND
- 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:
- Not sunny and not hot –> no beach.
- Not sunny but hot –> no beach.
- Sunny but not hot –> no beach.
- Sunny and hot –> beach time.
A more formal way to represent these scenarios would be to build a truth table like this:
Is sunny | Is hot | Is sunny and is hot (beach time) |
---|---|---|
false | false | false |
false | true | false |
true | false | false |
true | true | true |
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:
- Check the weather report to determine the truth values for “Is sunny” and “Is hot”.
- Use the truth table to find a row that matches the truth values from the weather report.
- 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:
A | B | A && B |
---|---|---|
false | false | false |
false | true | false |
true | false | false |
true | true | true |
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:
A | B | A || B |
---|---|---|
false | false | false |
false | true | true |
true | false | true |
true | true | true |
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 |
---|---|
false | true |
true | false |
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.
Next Chapter: If-Else Statements in Swift
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
In the next chapter, you are going to learn how to use boolean values and logical operators to introduce logic in your code.
Next Chapter: If-Else Statements in Swift
About the Author
- I'm an entrepreneur and a blogger from Finland. My goal is to make coding and tech easier for you with comprehensive guides and reviews.
Recent Posts
Artificial Intelligence2023.05.16Humata AI Review (2023): Best PDF Analyzer (to Understand Files)
Python2023.05.139 Tips to Use ChatGPT to Write Code (in 2023)
Artificial Intelligence2023.04.1114 Best AI Website Builders of 2023 (Free & Paid Solutions)
JavaScript2023.02.16How to Pass a Variable from HTML Page to Another (w/ JavaScript)