Let’s jump right in!
Comparison operators
Comparison operators are very similar to arithmetic operators. The vocab bar below shows the conditional operators and their functions:
Vocab Bar | |
---|---|
== | Checks if both operands are exactly equal to each other |
< | Checks if the operand on the left of the < is less than the one on the right. |
<= | Checks if the operand on the left of the < =is less than or exactly equal to the operand on the right. |
> | Checks if the operand on the left of the > is greater than the one on the right. |
>= | Checks if the operand on the left of the >= is greater than or exactly equal to the operand on the right. |
!= | Checks if the operand on the left is not equal to the right. |
Comparison operators require two variables or values that you want them to compare. These variables of course, need to be comparable. Variables need to be of the same type to be comparable. When a comparison operation is completed, we get a boolean output. In other words, the result of a comparison operation is always a value of type Bool
:true
or false
.
For example, the >
operator checks if the operand on the left of the >
is greater than the operand on the right. If the left operand is greater than the right operand, the value given by the operation is true
. Else, it is false
.
Examples:
var trueOrFalse = (2 == 3) //This will make trueOrFalse have a value of false
trueOrFalse = (2 != 3) //This will make trueOrFalse have a value of true
trueOrFalse = (2 < 3) //This will make trueOrFalse have a value of true
trueOrFalse = (2 <= 3) //This will make trueOrFalse have a value of true
Do not go beyond without understanding the concepts given above.
Type declaration
You might have noticed that I didn’t tell the compiler that the variable trueOrFalse
was a Bool
. But the we get no errors. This is because Swift’s compiler is smart and assumes the type of variable you are creating from the nature of the value you assign to it. If you give it an Int
value like 98
, it will assume that the variable is of Int
type even if you don’t declare it to be of Int
type.
Conditional statements
The most important place where comparison operators are used is in conditional statements. Conditional statements are lines of code that run only when specific conditions are satisfied. An example of a place where conditional statements can be used is when you want to decide if a person is eligible to vote. The condition her is “Is the person over the age of eighteen”. If that condition is true, only then we let the person vote. Otherwise, the person doesn’t get to vote. Let’s break up this situation:
If the age of a person is greater than or equal to 18{
Let them vote
}
If the condition is not satisfied{
Do not let them vote
}
What you saw above, was the English version of an if-else statement. Now let’s go to the Swift version of it. Let’s say that age
is the variable that stores the age of the person you are checking for eligibility to vote and canVote
is the variable you set to true
if a person is eligible.Here is the Swift version of the if-else statement:
if age >= 18 {
canVote=true //person can vote
}
else{ //If the condition next to the “if” is not true
canVote=false //person can't vote
}
Lines of code that are enclosed in between {
and }
after a condition are what get executed when the condition is true. The else
keyword represents a condition when the condition given after the if
keyword is false. if
statements can exist without an else
to accompany them. But if you use an else
statement, it will run only if the if
condition directly above it is not true. It doesn’t consider any other if
.
Written by Valliappan Valliappan
Notice any mistakes? Please email us at learn@teamscode.com so that we can fix any inaccuracies.