Swift 101: Basic Operators
Tags: learning, swift, mobile, beginners
Published: 2024-05-23 13:48:07

#<h1> Hola Mundo! </h1>
<Enter>

Welcome to a new article in a series of [Swift 101 notes](https://dev.to/silviaespanagil/swift-101-getting-into-ios-development-gji) 📝 I did while learning the language and I decided to share them because why not?.

If you're new to Swift or interested in learning more about this language, I invite you to follow my series!🙊

In this chapter, I'll be sharing a little bit about `Basic Operators in Swift`.
___

In the [last chapter](https://dev.to/silviaespanagil/swift-101-understanding-types-variables-and-constants-l9h), we discussed constants and variables, which store values for us to use in our code.

When working with these values, we often need them to interact with each other. So, how do we accomplish this? 
✨​ Enter operators✨​ 

Operators are special symbols that enable us to perform various operations on our variables. These operations can assign or change the value of a variable, allow us to compare one variable with another, or apply logic in our code.

___
###Arithmetic operators ➕​
Even if the "Arithmetic" name sound kinda scary, these operators are pretty simple and easy to understand. 

Arithmetic operators are basically symbols that allow us to create mathematical calculations, additions, subtractions, multiplications, and divisions.

| Operation | Action | Example | Result |
| --- | --- | --- | --- |
| Addition + | Addition between two values | var addition = 34 + 4 | print(addition) Output: 38 |
| Subtraction - | Subtract operation between values | var subtraction = 34 - 4 | print(subtraction) Output: 30 |
| Multiplication * | Multiplication of values | var multiplication = 30 * 2 | print(multiplication) Output: 60 |
| Division / | Division calculation | var division = 30 / 2 | print(division) Output: 15 |

And yes, the logic says that this kind of operator only works with numeric types. However, Swift also allows us to do an addition operation with String-type variables. This is called **string concatenation** and it will put together in order different strings.

```swift
var magicSpell = "Accio 🪄 "
var object = "Nimbus 2000"
var completeSpell = magicSpell + object
print(completeSpell) 
// Output: "Accio 🪄  Nimbus 2000"
```

Please notice that ✨this is not the only way✨ to put two strings together but as this is a `Basic Operators` article we will not get in-depth with other ways to do so.
___
###Modulo operator

Modulo or remainder operator `%` returns the value that is left after you divide two numbers.

```swift
var moduleA = 10 % 3
var moduleB = 10 % 5
print(moduleA) // 1
print(moduleB) // 0
```

Understanding modulo. If we divide `10 / 3` the quotient is `3` with a remainder of `1`. This means that if we do `10 % 3`, the result is `1`.

Similarly, if we divide `10 / 5` the quotient is `2` which is already a whole number, meaning that there is no remainder from the operation. Therefore, 10 % 5 results in 0.

For me, this was one of the hardest operators to understand🤦🏽‍♀️, so I wrote down an example that helped me:

Imagine we have ten apples 🍎​🍎​🍎​🍎​🍎​🍎​🍎​🍎​🍎​🍎​ and there are three very hungry people, so we want to share the apples between all of us. 
​🧑‍🎤​ - 🍎​🍎​🍎
👨‍🎤 - 🍎​🍎​🍎
​👩‍🎤​ - 🍎​🍎​🍎
If we want all of us to have the same amount of apples, we will each eat three apples. And there will be an extra apple 🍎 all lonely without a human to eat it. Well, that apple is the module of the first ten! Is the remainder of `10 % 3`
___
###Assignment and compound operators
This kind of operator will assign a value to a variable or constant.

The mother of the assignment operators is the **equal operator** 🟰​. We have already used it in the previous chapter and is the one that says to Swift this value should be assigned to this variable or constant

```swift
var variableName: <Variable Type> = <Initial value>
var anotherVariable: <Variable Type>
```
However, the equal operator can be mixed with the arithmetic operators so it does a mathematical calculation and assign the value at the same time.

####Equal `=`
The value from the right will be assigned to the one on the left.
```swift
var a = 10
var b = 100
a = b
print(a) Output: 100
```

####Addition `+=`
The value from the right will be added and assigned to the one on the left.
```swift
var a = 10
var b = 5
a += b
print(a) Output: 15
```

####Subtraction `-=`
The value from the right will be subtracted and assigned to the one on the left.
```swift
var a = 10
var b = 5
a -= b
print(a) Output: 5
```

####Multiplication `*=`
The value from the right will be multiplied and assigned to the one on the left.
```swift
var a = 10
var b = 5
a *= b
print(a) Output: 50
```

####Division `/=`
The value from the right will be divided and assigned to the one on the left.
```swift
var a = 10
var b = 5
a /= b
print(a) Output: 2
```

####Modulo `%=`
It will calculate the modulo and assign it to the variable on the left.
```swift
var a = 10
var b = 5
a %= b
print(a) Output: 0
```
___

###Comparison operators
These operators allow us, as the name states, to compare the value from the left side to the one on the right to check if the condition is true or false.

#### Equal to `==`

The value on the right is equal to the one on the left.

```swift
let a = 10
let b = 100
let c = (a == b)
print(c)  Output: false
```

#### Not equal to `!=`

The value on the right is **not equal** to the one on the left.

```swift
let a = 10
let b = 100
let c = (a != b)
print(c)  Output: true
```
#### Greater than `>`

The value on the left is **greater** than the one on the right.

```swift
let a = 10
let b = 100
let c = (a > b)
print(c) Output: false
```
#### Less than `<`

The value on the left is **smaller** than the one on the right.

```swift
let a = 10
let b = 100
let c = (a < b)
print(c)  Output: true
```
#### Greater or equal to `>=`

The value on the left is **greater than or equal to** the one on the right.

```swift
let a = 100
let b = 100
let c = (a >= b)
print(c)  Output: true
```
#### Less or equal to `<=`

The value on the left is **smaller than or equal to** the one on the right.

```swift
let a = 100
let b = 10
let c = (a <= b)
print(c)  Output: false
```
___

###Logical operators

Logical operators allow us to check or set some rules based on logic. They compare **two different conditions** and tell us if the comparison between them is true or false. 
Logical operators are particularly useful for making decisions in our code, as they enable more complex condition checking.

There are three Logical Operators:

- **And `&&`**: Check if both conditions are true → This **and** that are true
```swift
let a = 10
let b = 10
let c = ((a <= b) && (a == b))
print(c) // Output: true
```
In the example, we check if `a`(10) is less or equal than `b`(10) **and** if `a`(10) is the same as `b`(10). As both of the conditions are true, then the output would be `true`.

- **Or `||`**: Check if at least one of the conditions are true  → This **or** that are true
```swift
let a = 10
let b = 100
let c = ((a <= b) || (a == b))
print(c) // Output: true
```
In the example, we check if `a`(10) is less or equal than `b`(100) **or** if `a`(100) is the same as `b`(10). In this case, the first condition is true, but the second is false. As we want to know if one or the other is true, effectively, one of them is true so the output will also be `true`.

- **Not `!`**: Returns true if the operand is false and vice versa
```swift
print(!true) // Output: false
```
In the example, we print the negation of true which is false
___
###Want to keep learning about Swift? 

So far, we've looked at what Swift is, explored types, variables, and constants, and now we've seen how basic operators can help us perform calculations, compare values, and make decisions in our code. But... there's more to come! 👩🏽‍💻

This is a full series on [Swift 101](https://dev.to/silviaespanagil/swift-101-getting-into-ios-development-gji). The next chapter will be the first part of Collections and Collection Types, so I hope to see you there 🫶!

If you enjoyed this, please share, like, and comment. I hope this can be useful to someone and that it will inspire more people to learn and code with Swift.