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 = 11let y = 3// additionprint (x + y)// subtractionprint (x - y)// integer divisionprint (x / y)// multiplicationprint (x * y)// moduloprint (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.0let y = 2.0// float divisionprint (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 avar a = 7// Assigns 2 to bvar b = 2// Addition Assignment - assigns the sum of a and b to aa += b // a = a + b// Subtractiontion Assignment - assigns the difference between a and b to aa -= b // a = a - b// Division Assignment - assigns the quotient after the division between a and b to aa /= b // a = a / b// Multiplication Assignment - assign the product of a and b to aa *= b // a = a * b// Modulo Assignment - assign the remainder after the division between a and b to aa %= 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 = 9var b = 2// Greater than operatorprint(a > b)// Less than operatorprint(a < b)// Greater than or equal to operatorprint(a >= b)// Less than or equal to operatorprint(a <= b)// Equal to operatorprint(a == b)// Not equal to operatorprint(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 = truelet secondCondition = falseprint(firstCondition && secondCondition)print(firstCondition || secondCondition)print(!secondCondition)