Operators are special symbols used to perform various operations on variables and values. Swift provides a range of operators found in already known languages such as C and improves on their functionalities.
Operators in Swift are divided into several categories based on the kinds of operations they perform.
The four standard arithmetic operators are supported for all number types in Swift. Aside from the four standard arithmetic operators, they also give support to the Modulo or Remainder operator.
The Modulo operator is an arithmetic operator that returns the remainder of an integer division. The Modulo operator is denoted with the symbol and can only be used with integers.
The 5 arithmetic operators in Swift are:
These operators are described in the example below:
let x = 11 let y = 3 // addition print (x + y) // subtraction print (x - y) // integer division print (x / y) // multiplication print (x * y) // modulo print (x % y)
The division operator only returns the quotient if the two numbers to be divided are integers. The division operator in the example above exhibits this behavior. However, if the division operator is used to divide floats, meaning floating-point numbers, the exact result will be returned.
An example is shown below:
let x = 7.0 let y = 2.0 // float division print (x / y)
Assignment operators are used to initialize or update the values of variables. Assignment operators are denoted with the =
symbol.
The syntax below assigns the value of to the variable a
.
var a = 10
Assignment operators can be of various types, which include:
These operators are used in the example below:
// Assigns 7 to a var a = 7 // Assigns 2 to b var b = 2 // Addition Assignment - assigns the sum of a and b to a a += b // a = a + b // Subtractiontion Assignment - assigns the difference between a and b to a a -= b // a = a - b // Division Assignment - assigns the quotient after the division between a and b to a a /= b // a = a / b // Multiplication Assignment - assign the product of a and b to a a *= b // a = a * b // Modulo Assignment - assign the remainder after the division between a and b to a a %= b // a = a % b
Comparison operators are used to compare two variables or values. Comparison operators return a Boolean depending on the result and are used in decision-making and loops. Swift supports different comparison operators, such as the following:
>
)<
)>=
)<=
)==
)!=
)The example below demonstrates how to use these comparison operators:
var a = 9 var b = 2 // 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) // Equal to operator print(a == b) // Not equal to operator print(a != b)
Logical operators are important in decision-making. They are used to combine the Boolean logic values and check if an expression is true or false.
The logical operators supported by Swift are:
&&
) returns true
only if both operands are true. Else, it returns false
.||
) returns true
if at least one of the operands is true
. Else, it returns false
.!
) returns true
if the operand is false
and vice-versa.An example is shown below:
let firstCondition = true let secondCondition = false print(firstCondition && secondCondition) print(firstCondition || secondCondition) print(!secondCondition)
RELATED TAGS
CONTRIBUTOR
View all Courses