> For the complete documentation index, see [llms.txt](https://ios-course.cornellappdev.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ios-course.cornellappdev.com/resources/textbook/swift-basics/operators.md).

# Operators

Fall 2023 | Vin Bui

We have seen the four basic math operations in elementary school: addition, subtraction, multiplication, and division. In Swift, we can use operators to perform these operations.

### Basic Operators

```swift
var a = 0
a = a + 10
a = a - 5
a = a * a
a = a / 10
```

<figure><img src="https://1509678725-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lwk7443W4ukbAF9S07e%2Fuploads%2Fh8QnGPO1xTJ29kR4O6DN%2FUntitled.png?alt=media&amp;token=4a7a8f1a-b9d3-4dce-b8c3-98e18cf8b5cb" alt=""><figcaption></figcaption></figure>

The following lines are equivalent:

<figure><img src="https://1509678725-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lwk7443W4ukbAF9S07e%2Fuploads%2FrFL9F6yFtHOGkwBztG44%2FUntitled.png?alt=media&amp;token=a534677b-73d8-4704-98b8-ed72ce0ac47f" alt=""><figcaption></figcaption></figure>

These operators are self-explanatory; however, if we were to take a closer look at the the line `a = a / 10` we can notice that the output is `2` instead of `2.5`. The reason for this is because the type of `a` is an `Int`. If we were to perform these operations on `a`, then we must also use an `Int`.

<figure><img src="https://1509678725-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lwk7443W4ukbAF9S07e%2Fuploads%2Fs2h00ZeCKyOGrEA7MtZ8%2FUntitled.png?alt=media&amp;token=7545d270-f7f5-4334-b845-d56e7d144e26" alt=""><figcaption></figcaption></figure>

Then, how do we get the value `2.5`? Because the type of `a` is an `Int`, then we must introduce a new variable of type `Double` or `Float` since we cannot change the type of a variable once initialized. We would also need to make sure that the values in which we apply these operators on must also be a `Double` or `Float`.

<figure><img src="https://1509678725-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lwk7443W4ukbAF9S07e%2Fuploads%2Fh3XTdb4MBVRzWzLJsl3q%2FUntitled.png?alt=media&amp;token=6b7d119a-931f-47d8-a2b9-7a6daa03d038" alt=""><figcaption></figcaption></figure>

Let's take a look at the line `Double(a)`. This is known as **type casting**. Because `a` is an `Int` and we needed a `Double`, `Double(a)` converts the value `2` to `2.0`. Note that this **does not** change the type of `a`. It only produces a value to be used for that operation.

One more common operator we may see is the **modulus** operator (`%`). This is similar to the `/` operator except we return the remainder.

<figure><img src="https://1509678725-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lwk7443W4ukbAF9S07e%2Fuploads%2FmbCb9AYq28jngsumDbqy%2FUntitled.png?alt=media&amp;token=e3390af9-c142-45be-9cf6-4b4d0354ca45" alt=""><figcaption></figcaption></figure>

### Common Operators

The following is a list of common operators that we are likely to use.

| `>`  | greater than             | `\|\|` | or  |
| ---- | ------------------------ | ------ | --- |
| `>=` | greater than or equal to | `&&`   | and |
| `<`  | less than                | `!`    | not |
| `<=` | less than or equal to    |        |     |
| `==` | equal to                 |        |     |
| `!=` | not equal to             |        |     |

### String Interpolation

**String interpolation** is a way of combining variables and constants inside a string. Take a look at this example:

```swift
var name = "Vin"
"My name is \(name)."
```

<figure><img src="https://1509678725-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lwk7443W4ukbAF9S07e%2Fuploads%2FnxyjnSuAT1DjJgyPOpom%2FUntitled.png?alt=media&amp;token=425aa670-ece5-4c9f-a965-f3d9fe4cae64" alt=""><figcaption></figcaption></figure>

Of course, we could have used the `+` operator to concatenate these strings together.

<figure><img src="https://1509678725-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lwk7443W4ukbAF9S07e%2Fuploads%2FRFhRTXqWPUdrtxtAFYtW%2FUntitled.png?alt=media&amp;token=b62f9123-deb1-4558-b623-445e7d54aecf" alt=""><figcaption></figcaption></figure>

The problem with this approach is efficiency especially if we want to concatenate multiple variables. Another issue with using `+` is that Swift does not allow types such as `Int` or `Float` to be glued with a `String`.

<figure><img src="https://1509678725-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lwk7443W4ukbAF9S07e%2Fuploads%2FVDQzaoJiwSfCg0mNnIuk%2FUntitled.png?alt=media&amp;token=12003029-b41a-4ddf-b4d5-5292c86845b3" alt=""><figcaption></figcaption></figure>

We could cast `age` to a `String` but that would be expensive.

<figure><img src="https://1509678725-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lwk7443W4ukbAF9S07e%2Fuploads%2FbGvjyPCfcBAa1oBdJZUY%2FUntitled.png?alt=media&amp;token=b76f1935-f9c3-47f7-af02-d33a9da6189a" alt=""><figcaption></figcaption></figure>

Using string interpolation is a lot more efficient and looks cleaner too!

<figure><img src="https://1509678725-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lwk7443W4ukbAF9S07e%2Fuploads%2FTg5QCLomIZZ01w57Xfdy%2FUntitled.png?alt=media&amp;token=b6dd8f01-21fd-4288-af3a-ce3ebef94b0e" alt=""><figcaption></figcaption></figure>
