There are various operators supported by Swift. These symbols are used to carry out logical and mathematical operations in a program. These symbols are used to join individual constants and variables to form expressions and to perform specific operations. In this chapter, you will learn about all the operators that Swift programming language support for its coding.
What are the operators in Swift?
Operators are special symbols or phrases that programmers use to check, combine, or change values. For example, the multiplication operator (*) multiplies two numbers together. Another complex example includes the logical AND operator && (as in if username && password) and the increment operator ++i, which is a shortcut to increase the value of i by 1. Swift supports most standard C operators and eliminates several common coding errors.
Types of Operators
Here's a list of different types of Swift operators that you will learn in this tutorial.
- Arithmetic operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
1. Swift Arithmetic Operators
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, etc. For example,
var sub = 10 - 5 // 5
Here, - is an arithmetic operator that subtracts two values or variables.
Operator Operation Example
- + : Addition (5 + 2 = 7)
- - : Subtraction (4 - 2 = 2)
- * : Multiplication (2 * 3 = 6)
- / : Division (4 / 2 = 2)
- % : Modulo (5 % 2 = 1)
Example 1: Arithmetic Operators in Swift
var a = 7var b = 2// additionprint (a + b)// subtractionprint (a - b)// multiplicationprint (a * b)
Output
9514
In the above example, we have used
- + to add a and b
- - to subtract b from a
- * to multiply a and b
- / Division Operator
The / operator performs division between two numbers. However, if two numbers are integers, we will only get the quotient. For example,
// returns quotient only
- 7 / 2 = 3
If we use the / operator with floating-point numbers, we will get the actual result. For example,
// perform division
- 7.0 / 3.0 = 3.5
% Modulo Operator
The modulo operator % computes the remainder. For example,
- print(9 % 4) // 1
Note: The % operator can only be used with integers.
2. Swift Assignment Operators
Assignment operators are used to assign values to variables. For example,
// assign 5 to x
- var x = 5
Here, = is an assignment operator that assigns 5 to x. Here's a list of different assignment operators available in Swift.
- = : Assignment Operator (a = 7)
- += : Addition Assignment (a += 1 // a = a + 1)
- -= : Subtraction Assignment (a -= 3 // a = a - 3)
- *= : Multiplication Assignment (a *= 4 // a = a * 4)
- /= : Division Assignment (a /= 3 // a = a / 3)
- %= : Remainder Assignment (a %= 10 // a = a % 10)
Example 2: Assignment Operators
// assign 10 to avar a = 10// assign 5 to bvar b = 5// assign the sum of a and b to aa += b // a = a + bprint(a)
Output
15
3. Swift Comparison Operators
Comparison operators compare two values/variables and return a boolean result: true or false. For example,
var a = 5, b =2print (a > b) // true
Here, the > comparison operator is used to compare whether a is greater than b or not.
Operator Meaning Example
- == : Is Equal To (3 == 5 gives us false)
- != : Not Equal To (3 != 5 gives us true)
- > : Greater Than (3 > 5 gives us false)
- < : Less Than (3 < 5 gives us true)
- >= : Greater Than or Equal To (3 >= 5 give us false)
- <= : Less Than or Equal To (3 <= 5 gives us true)
Example 3: Comparison Operators
var a = 5, b = 2
// equal to operator
print(a == b)
// not equal to operator
print(a != b)
// greater than operator
print(a > b)
// less than operator
print(a < b)
// greater than or equal to operator
print(a >= b)
// less than or equal to operator
print(a <= b)
Output
falsetruetruefalsetruefalse
Note: Comparison operators are used in decision-making and loops. We'll discuss more of the comparison operator in later tutorials.
4. Swift Logical Operators
Logical operators are used to check whether an expression is true or false. They are used in decision-making. For example,
var a = 5, b = 6print((a > 2) && (b >= 6)) // true
Here, && is the logical operator AND. Since both a > 2 and b >= 6 are true, the result is true.
- && : a && b (Logical AND: true only if both the operands are true)
- || : a || b (Logical OR: true if at least one of the operands is true)
- ! : !a (Logical NOT: true if the operand is false and vice-versa.)
Example 4: Logical Operators
// logical ANDprint(true && true) // trueprint(true && false) // false// logical ORprint(true || false) // true// logical NOTprint(!true) // false
Output
truefalsetruefalse
5. Swift Bitwise Operators
In Swift, bitwise operators are used to perform operations on individual bits.
- & : Binary AND
- | : Binary OR
- ^ : Binary XOR
- ~ : Binary One's Complement
- << : Binary Shift Left
- >> : Binary Shift Right