DEV Community

Cover image for Swift 101: Basic Operators
Silvia EspaΓ±a Gil
Silvia EspaΓ±a Gil

Posted on

Swift 101: Basic Operators

Hola Mundo!

Welcome to a new article in a series of Swift 101 notes πŸ“ 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, 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.

var magicSpell = "Accio πŸͺ„ "
var object = "Nimbus 2000"
var completeSpell = magicSpell + object
print(completeSpell) 
// Output: "Accio πŸͺ„  Nimbus 2000"
Enter fullscreen mode Exit fullscreen mode

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.

var moduleA = 10 % 3
var moduleB = 10 % 5
print(moduleA) // 1
print(moduleB) // 0
Enter fullscreen mode Exit fullscreen mode

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

var variableName: <Variable Type> = <Initial value>
var anotherVariable: <Variable Type>
Enter fullscreen mode Exit fullscreen mode

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.

var a = 10
var b = 100
a = b
print(a) Output: 100
Enter fullscreen mode Exit fullscreen mode

Addition +=

The value from the right will be added and assigned to the one on the left.

var a = 10
var b = 5
a += b
print(a) Output: 15
Enter fullscreen mode Exit fullscreen mode

Subtraction -=

The value from the right will be subtracted and assigned to the one on the left.

var a = 10
var b = 5
a -= b
print(a) Output: 5
Enter fullscreen mode Exit fullscreen mode

Multiplication *=

The value from the right will be multiplied and assigned to the one on the left.

var a = 10
var b = 5
a *= b
print(a) Output: 50
Enter fullscreen mode Exit fullscreen mode

Division /=

The value from the right will be divided and assigned to the one on the left.

var a = 10
var b = 5
a /= b
print(a) Output: 2
Enter fullscreen mode Exit fullscreen mode

Modulo %=

It will calculate the modulo and assign it to the variable on the left.

var a = 10
var b = 5
a %= b
print(a) Output: 0
Enter fullscreen mode Exit fullscreen mode

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.

let a = 10
let b = 100
let c = (a == b)
print(c)  Output: false
Enter fullscreen mode Exit fullscreen mode

Not equal to !=

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

let a = 10
let b = 100
let c = (a != b)
print(c)  Output: true
Enter fullscreen mode Exit fullscreen mode

Greater than >

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

let a = 10
let b = 100
let c = (a > b)
print(c) Output: false
Enter fullscreen mode Exit fullscreen mode

Less than <

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

let a = 10
let b = 100
let c = (a < b)
print(c)  Output: true
Enter fullscreen mode Exit fullscreen mode

Greater or equal to >=

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

let a = 100
let b = 100
let c = (a >= b)
print(c)  Output: true
Enter fullscreen mode Exit fullscreen mode

Less or equal to <=

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

let a = 100
let b = 10
let c = (a <= b)
print(c)  Output: false
Enter fullscreen mode Exit fullscreen mode

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
let a = 10
let b = 10
let c = ((a <= b) && (a == b))
print(c) // Output: true
Enter fullscreen mode Exit fullscreen mode

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
let a = 10
let b = 100
let c = ((a <= b) || (a == b))
print(c) // Output: true
Enter fullscreen mode Exit fullscreen mode

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
print(!true) // Output: false
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)