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.
Different Kinds of Operators
Operators are of three kinds. These are:
- Unary Operators: These operators operate on a
single target. These operators appear immediately before the
operands. There are two types of unary operators. They are -
- Unary prefix (example:a, !value)
- Unary postfix (example: x++)
- Binary Operators: These operators operate on two targets (like 6 + 2), i.e., they do their work on two operands.
- Ternary Operators: These operators operate on three targets/operands. Like C, Swift has only one ternary operator, the ternary conditional operator (a ? b : c).
Types of Swift Operators
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
- Misc Operators
- Range Operators
Swift supports the four standard arithmetic operators for all number types:
They are used to assign values to variables. These are:
|+=||Increments then assign|
|-=||Decrements then assign|
|*=||Multiplies then assign|
|/=||Divides then assigns|
|%=||Modulus then assigns|
|==||Is equal to|
|!=||Is not equal to|
|>=||Greater than or equal to|
|<=||Less than or equal to|
|&&||And operator. Performs logical conjunction on two
(if both expressions evaluate to True, result is True. If either expression evaluates to False, the result is False)
|||||Or operator. Performs a logical disjunction on two
(if either or both expressions evaluate to True, the result is True).
|!||Not operator. It performs logical negation on an expression.|
|&||Binary AND Operator|
||||Binary OR Operator|
|^||Binary XOR Operator|
|<<||Binary Left Shift Operator|
|>>||Binary Right Shift Operator|
The conditional Operator cones under this category. The structure is: Condition? X : Y, i.e., If Condition is true? Then 1st expression: Otherwise 2nd Expression All the above operators are common in C also and most of the C or C++ programmers are familiar with these operators. But Swift adds another type of operators that is called Range Operators which are shortcuts for expressing a range of values. They are of two types:
|Closed Range||This (x...y) defines x range that starts from x up to y, and includes the values x and y and the values in between. ..6 gives 1, 2, 3, 4,5 and 6|
|Half-Open Range||This (i..< j) defines i range that starts from I up to j, but does not include j. .< 6 gives 1, 2, 3, 4 and 5|