Already know you that which you need. -Yoda
In this lesson, we'll look at the Bool
data type and work with various comparison and logical operators.
- Explain that the
Bool
is a data type where the value can only be true or false - Create variables using the Boolean literal values,
true
orfalse
- Explain and use various comparison operators including "equal to," "greater than," and "less than"
- Explain and use various logical operators, including NOT, AND, and OR
Boolean is a fancy term meaning something that is either true or false. While numbers and math form the backbone of any piece of software, everything (and we do mean everything) in computers, software and microchips, breaks down to ones and zeros, true and false. While it isn't important to understand why that is and how exactly it works, it is important to understand how boolean expressions work so that your code will be able to do something interesting like this:
let name = "Luke"
let age = 34
let lengthOfLightSaber = 32.5
// name is a constant of type String with a value of "Luke"
// age is a constant of type Int with a value of 34
// lengthOfLightSaber is a constant of type Double with a value of 32.5
But.. what if we want to declare a variable as being true or false (type Bool).
let hasTheForce = true
// hasTheForce is a constant of type `Bool` with a value of true
Option clicking this constant in the playground file to see its type will show us this result:
Here is another example, except here we're creating a variable which will allow us to change the value.
var isHungry = false
// isHungry is a variable of type Bool with a value of false
isHungry = true
// isHungry now has a value of true
Going with the example above, our program would like to know if isHungry is true or false and based upon that value, it will do something. If the person is hungry, it will feed them. If not, it will do nothing.
Almost every program you write will need to take different courses of action based on boolean values, so it's important to have a really good grasp of what booleans and boolean expressions are as well as how to use them.
Swift recognizes a value as boolean if it sees true
or false
. You can implicitly declar a boolean variable let a = false
or explicitly declare a boolean variable let i:Bool = true
.
var a: Bool = true
// Explicitly declaring a variable called 'a' of type Bool to equal true
var b = false
// Implicitily declaring a variable called 'b' to equal false where 'b' is a variable
##Comparison The three main comparisons are "greater than" (>), "less than" (<), and "equals" (==). For exampe:
3 > 1 (true)
3 < 1 (false)
3 == 1 (false)
3 == 3 (true)
There is a special operator "not" (!) which is kind of like mathematical sarcasm (e.g. "Justin Bieber is cool... Not!). These operators can be combined to create three additional operators "greater than or equals" (>=), "less than or equals" (<=), and "not equals" (!=):
3 >= 4 // false
3 >= 3 // true
3 != 4 // true
3 != 3 // false
3 <= 4 // true
3 <= 3 // true
##Compound Comparison All of the examples above have been simple expressions. We can form complex boolean expressions, that is expressions which are the result of more than one comparison, with the logical operators "and" (&&) and "or" (||). For example:
(3 >= 3) && (3 <= 4) // true
(3 >= 3) || (3 <= 4) // true
(3 >= 3) && (3 > 4) // false
Here's another example:
var providedPassword = true
var passedRetinaScan = false
providedPassword && passedRetinaScan // false
providedPassword || passedRetinaScan // true
passedRetinaScan = true
providedPassword && passedRetinaScan // true
##Negation You can use the "not" (!) operator to invert the value of any boolean value or expression. Note that you can't have any space between the "!" and the value it's negating. For example:
! true (syntax error)
!true (false)
!false (true)
!(3 < 4) (false)
!(3 > 4) (true)
!((3 >= 3) && (3 <= 4)) (false)
!((3 >= 3) || (3 <= 4) ) (false)
!((3 >= 3) && (3 > 4)) (true)
You can mix-and-match these operators in all kinds of different ways:
(3 < 4) && !(3 <= 4) (false)
##Real World Usage When it comes to "real world" programming boolean expressions are used to check conditions so that you know how to proceed and/or what kind of UI to provide. For example:
var fileExists = false
In our program, if fileExists
is false
, we can show an alert to the user letting them know that there isn't a file and prompt them to look for another file. If fileExists
is true
then we can continue on with our program and show the contents of the file to the user.
View Booleans and Operators on Learn.co and start learning to code for free.