In programming, conditions are the most important part. When a program runs, it is constantly checking to execute one action or another, depending on the state of the variables we have implemented. For example, when a user enters their password, the program will verify that it is correct by comparing the entered password with the one stored in the system.
Result of a Comparison
It is very important to understand that the result of a comparison will always be of type Bool, meaning it will either be true or false; there is no middle ground.
Equal To
Letโs look at an example where two variables of type String are compared using the ==
operator.
var enteredPassword: String = "AAAA" // String type
var databasePassword: String = "AAAA" // String type
var isPasswordCorrect: Bool = enteredPassword == databasePassword // "True"
With this operator, we will get a result of type Bool, regardless of the type of the variables we are comparing.
We can compare any type of variable, but itโs important to always compare variables of the same type. For instance, we cannot compare a String with an Int.
Not Equal To
Using the !=
operator, we can check if two variables are different. If they are, the result will be true
.
var enteredAge: Int = 23 // Int type
var userAge: Int = 24 // Int type
var isAgeIncorrect: Bool = enteredAge != userAge // "True"
Other Operators
In addition to checking if a value is equal or not equal, we can also check if it is less than, greater than, greater than or equal to, and so on.
var enteredAge: Int = 23 // Int type
enteredAge > 10 // "True"
enteredAge < 20 // "False"
enteredAge <= 23 // "True"
enteredAge >= 30 // "False"
Comparing Bool Variables
To compare variables of type Bool, we can also use ==
or !=
, but not operators like <
or >
, because a variable of type Bool cannot be less than or greater than another, only equal to another.
Negation
The !
command in front of our Bool
variables or parentheses inverts their value:
var enteredAge: Int = 23 // Int type
var isAdult: Bool = enteredAge >= 18 // True
var isMinor: Bool = !isAdult // False
!(enteredAge > 10) // "False"
AND Operator (&&)
This operator checks that both sides of it are true
. This is exclusive to variables of type Bool
.
var isUserCorrect: Bool = true
var isPasswordCorrect: Bool = true
var isAgeCorrect: Bool = false
var areCredentialsCorrect: Bool = isUserCorrect && isPasswordCorrect // "True", since both "isUserCorrect" AND "isPasswordCorrect" are "True"
areCredentialsCorrect && isAgeCorrect // "False", since one of them is "False"
// Combinations
true && true // true
true && false // false
false && true // false
false && false // false
OR Operator (||)
This operator checks that at least one side of it is true
. This is exclusive to variables of type Bool
.
var isAdult: Bool = false
var hasParentalPermission: Bool = true
var canAccess: Bool = isAdult || hasParentalPermission // "True", since one of them is "True"
// Combinations
true || true // true
true || false // true
false || true // true
false || false // false
Combinations Are Infinite
Itโs important to know that you can compare more than two variables in the same line.
// Other combinations
true && true && false // false
true || false || false || false // true
// Use parentheses to resolve comparisons, just like in any mathematical operation.
false || (true && true) // true
Be the first to comment