> 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

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="/files/sqNm7jFtvSNJnyXoKOrQ" alt=""><figcaption></figcaption></figure>

The following lines are equivalent:

<figure><img src="/files/LkjcpQBSYKR1HS2uLjcb" 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="/files/CSUAbYbypRnZVlpMGuAS" 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="/files/x8yHph71k600AEYKTLhG" 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="/files/Q2WmtL08caQHUdcVOsy2" 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="/files/7AKqnyqKHxXMUuWAckg3" alt=""><figcaption></figcaption></figure>

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

<figure><img src="/files/iVEehJtv1b8ulgMFefsi" 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="/files/Fw0pRWPVMsVDvoJ3zjjN" alt=""><figcaption></figcaption></figure>

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

<figure><img src="/files/rnv1924CeVrLDaIxNYeF" alt=""><figcaption></figcaption></figure>

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

<figure><img src="/files/6SCfIQWvroKdimd80s3K" alt=""><figcaption></figcaption></figure>
