Swift Comparison Operators: iOS Development for Beginners - codingem.com

Comparison Operators

In Swift, you can compare values using the comparison operators, such as the greater-than operator.

For example, let’s check if 1 is less than 2:

let a = 1
let b = 2

print(a < b)

Output:

true

Here is a table of all the comparison operators in Swift:

Operator Name Description
== equals Checks if two values are equal to one another.
!= not equal Checks if two values are not equal to one another.
< less than Checks if the left-hand side is less than the value on the right-hand side.
<= less than or equal Checks if the left-hand side is less than or equal to the value on the right-hand side.
> greater than Checks if the left-hand side is greater than the value on the right-hand side.
>= greater than or equal Checks if the left-hand side is greater than or equal to the value on the right-hand side.

In this chapter, you learn how to perform comparisons in Swift. You are going to learn about the operators in the above table in more detail with useful examples.

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!

Comparison Operators in Swift

The Swift comparison operators are pretty intuitive and similar to the comparison operators you have seen in maths.

However, because this is a beginner guide, we are going to take a closer look at each of these operators.

== Operator

In Swift, you use the single equal sign as an assignment operator.

Thus, it cannot be used as a comparison operator. Instead, to compare equality, you need to use the double equals operator (==).

This is one that most beginners forget, and it can cause mysterious errors when running the program.

Let’s see an example where we compare two integers to see if they are equal:

let n = 10
let m = 10

print(n == m)

Output:

true

Remember to write these examples in your Playground file, and feel free to change the values to see the result change.

Next, let’s talk about the inequality operator.

!= Operator

To check if two values are not equal, use the inequality operator (!=) by placing it in between two comparable values.

For example, let’s compare two variables to see if they are not equal to one another:

var x = 1.02
var y = 1.03

var notEqual: Bool = x != y

print(notEqual)

Output:

true

Now you know how to compare equality and inequality.

Next, let’s start comparing the magnitudes of values.

< Operator

In Swift, you can check if value a is less than value b with the less than operator (a < b).

For example, let’s check if a person is younger than another:

let ageAlice = 32
let ageBob = 21

print(ageAlice < ageBob)

Output:

false

<= Operator

In Swift, you can check if value a is less than or equal to value b with the less than or equal to operator (a <= b).

This operator looks like an arrow that points left. However, it is a combination of the less-than operator and the equality operator.

For example, let’s see if the bank balance is less than or equal to a limit:

let balance = 100.00
let limit = 100.00

print(balance <= limit)

Output:

true

> Operator

In Swift, you can check if value a is greater than value b with the greater than operator (a > b).

For example, let’s check if a person is taller than another:

let heightAlice = 1.72
let heightBobby = 1.92

let bobbyIsTaller = heightBobby > heightAlice

print(bobbyIsTaller)

Output:

true

>= Operator

In Swift, you can check if value a is greater than or equal to value b with the greater than or equal to operator (a >= b).

For example, given a distance in kilometers, let’s check if the distance is greater than or equal to 2 miles:

let distanceKilometers = 2.7
let distanceMiles = distanceKilometers / 1.60934

print(distanceMiles >= 3.0)

Output:

false

Next Chapter: Logical Operators in Swift

Conclusion

In this chapter, you learned how to use the comparison operators in Swift.

To recap, there are 6 comparison operators in Swift:

  1. == for checking equality.
  2. != for checking if two values are not equal.
  3. < for checking if a value is less than another.
  4. <= for checking if a value is less than or equal to another.
  5. > for checking if a value is greater than another.
  6. >= for checking if a value is greater than or equal to another.

These operators are useful when combining numbers (or strings) in Swift.

In the next chapter, you are going to take the first steps toward implementing logic into your code by using logical operators.

Next Chapter: Logical Operators in Swift

About the Author

Artturi Jalli
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.