What is Comparsion and Logical Operator and what is it for?

Comparsion operator in Swift

Tested on: Xcode 12.2, Swift 5.3

Comparison operators are used to compare value, any value numerical or non-numerical and the value return is True orFalse. True and False are a Swift date type called Bool in short for Boolean. You will learn about Booleans, Comparsion andLogical Operator. This is an important concept because this help you to tell your applicaiton to do different task in different scenarios.

  1. What is boolean Data type?
  2. What is a Comparsion operator?
  3. How to use Comparsion operator with String?
  4. What is a Logical operator?





1. What is boolean Data type?

When you are comparing two value such as A equal to B. The answer is either true or false and these data types are called boolean. Boolean is a value that contains only true or false.

Below is how you use Boolean in Swift

Swift Code :

let trueValue = true
let falseValue = false

Toggle

A Bool often is to turn off or on a state. For example, a Bool represents if a phone is in dark mode or not. A handy method called toggle allows you to change between the two states.

Swift Code :

let trueValue = true
trueValue.toggle()
print( trueValue )

Output :

false




2. What is Comparsion operators?

Booleans are commonly used as a result of comparing two or more values. For example, when comparing whether 1 is equal to 2, it will return false because 1 and 2 are different thing.

Some of these operators should be familar to you when you are studying math in school. equal to, not equal to, greater than, greater and equal to, lesser than and lesser and equal to.


Equal to

In Swift, an equality operator is ==and this is to check whether the value is the same or not. The example below check whether 5 is the same as 5. Indeed, it is true!

Swift Code :

print( 5 == 5 )

Output :

true


Not Equal to

Similary, you can check if two values are not equal.

Swift Code :

print( 5 != 5 )

Output :

false


Greater then or greater and equal to

Swift Code :

print( 10 > 5 )
print( 5 >= 5 )

Output :

true
true


Lesser then or lesser and equal to

Swift Code :

print( 10 < 5 )
print( 4 <= 5 )

Output :

false
true


! operator

This is also called a not-operator. This change a true to a false and false to a true.

Swift Code :

print( !(5 == 5))

Output :

true




3. Using Compasion operators for String datatype.

Sometimes you want to compare if the two strings are the same or equal. You can use the same method as above and apply it to string.

Swift Code :

var username = "Wes"
print( username == "WesChua" )

Output :

false


Just like number, you can use greater than or lesser than with String. If you look at the ASCII Table, "A" has a number of 65 while "a" is 97. Therefore, the follow example below is false. No worries if you don't know, anything about ASCII Table. You can Google it and read up more about it if you want to.

Swift Code :

print( "bAc" > "bac" )

Output :

true




3. What is a Logical operator?

The example above only test for one condition. However, sometimes, you want to combine multiple comparisons and one way to do is to use comparison operators is to use logical operators which is AND or OR.

AND (&&) - returns true when all the conditions are met.

OR (||) - returns true when one of the conditions are met.


AND (&&) Operator

In Swift the operator for AND is &&.

"bAc" < "bac" is true but "abc" is not equal to "abC". Therefore, is result is false because only one condition is met and not all.

Swift Code :

print( "bAc" > "bac" && "abc" == "abC" )

Output :

false


OR (||) Operator

In Swift, the operator for OR is ||.

5 > 6 is false but 6 is equal to 6. Therefore, is result is true because one of the conditions is met.

Swift Code :

print( 5 > 6 || 6 == 6 )

Output :

false


Chaining

Of course, you can chain mutiple logical operator

Swift Code :

print( 5 > 6 || 6 == 6 && 2 == 6 )

Output :

false




Now, you know about Bool, Comparsion and Logical. You can combine these knowledge and If statement to create a logical in your app. These allows your app you response different style in different situations.