Swift: Comparison Operators

Before entering into the core of programming and see more complex functions such as the If…else, let’s first take a look at the different types of comparisons that exist in Swift.

There are several ways to compare for example 3 numbers. Let’s declare for example 2 constants as follows:

let num1 : Int = 1

let num2 : Int = 2

let num3 : Int = 3

Now let’s take a look at all the comparison operators that Swift allows us to employ:

  • Test if num1 is equal to num2: num1 == num2 (we used a double “=” sign!)
  • Test if num1 is higher (lower) than num2: num1 > num2 (num1 < num2)
  • Test if num1 is higher or equal (lower or equal) than num2: num1 >= num2 (num1 <= num2)
  • Test if num1 is different than num2: num1 != num2

We can also test multiple comparison operators at the same time. For that we 2 options:

  • either we want that both conditions are respected, in this case we use the AND, which is represented in SWIFT by “&&“.
  • either we want that one of the multiple conditions are respected and in this case we use the OR, which is represented in SWIFT by “||”.
Advertisements

One thought on “Swift: Comparison Operators

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: