Swift Types of Operators
Operators are the building blocks of any programming language, and Swift is no exception. In Swift, operators are used to perform a wide variety of tasks, such as assigning values to variables, performing arithmetic operations, comparing values, and more. In this article, we will discuss the different types of operators in Swift with examples and output.
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations on numerical values. Swift supports all the standard arithmetic operators, including addition, subtraction, multiplication, and division. The following table shows the arithmetic operators in Swift.
Operator | Example | Description |
---|---|---|
+ | 3 + 4 | Addition |
- | 7 - 2 | Subtraction |
* | 5 * 6 | Multiplication |
/ | 9 / 3 | Division |
% | 8 % 3 | Modulo (Remainder) |
Let's look at some examples:
1let x = 7 2let y = 3 3print(x + y) // Output: 10 4print(x - y) // Output: 4 5print(x * y) // Output: 21 6print(x / y) // Output: 2 7print(x % y) // Output: 1
Comparison Operators
Comparison operators are used to compare two values and return a Boolean value (true or false). The following table shows the comparison operators in Swift.
Operator | Example | Description |
---|---|---|
== | 3 == 3 | Equal to |
!= | 4 != 6 | Not equal to |
> | 5 > 3 | Greater than |
< | 2 < 7 | Less than |
>= | 8 >= 6 | Greater than or equal to |
<= | 2 <= 2 | Less than or equal to |
Let's look at some examples:
1let a = 5 2let b = 7 3print(a == b) // Output: false 4print(a != b) // Output: true 5print(a > b) // Output: false 6print(a < b) // Output: true 7print(a >= b) // Output: false 8print(a <= b) // Output: true
Logical Operators
Logical operators are used to combine two or more conditions and return a Boolean value (true or false). The following table shows the logical operators in Swift.
Operator | Example | Description |
---|---|---|
&& | true && false | Logical AND |
|| | true || false | Logical OR |
! | !true | Logical NOT |
Let's look at some examples:
1let m = true 2let n = false 3print(m && n) // Output: false 4print(m \|\| n) // Output: true 5print(!m) // Output: false
Assignment Operators
Assignment operators are used to assign values to variables. The following table shows the assignment operators in Swift.
Here is the markdown table format of the given Operators table:
Operator | Example | Description |
---|---|---|
= | x = 3 | Assigns the value 3 to x |
+= | x += 2 | Adds 2 to the value of x |
-= | x -= 4 | Subtracts 4 from the value of x |
*= | x *= 5 | Multiplies the value of x by 5 |
/= | x /= 2 | Divides the value of x by 2 |
%= | x %= 3 | Takes the modulo of the value of x with 3 |
Here is an example:
1var x = 3 2print(x) // Output: 3
In the above example, we have declared a variable x
and assigned it a value of 3
. We have then printed the value of x
to the console, which will output 3
.
Logical Operators
Logical operators are used to perform logical operations on Boolean values. Swift provides three logical operators: &&
(logical AND), ||
(logical OR), and !
(logical NOT).
Operator | Example | Description |
---|---|---|
&& | true && true | will return true if both are true |
|| | true || true | will return true if atleast one is true |
! | !false | will flip the value |
1let a = true 2let b = false 3 4if a && b { 5 print("This will not be printed.") 6} 7 8if a || b { 9 print("This will be printed.") 10} 11 12if !a { 13 print("This will not be printed.") 14}
In the above example, we have declared two Boolean variables a
and b
. We have then used the logical AND (&&
) operator to test if both a
and b
are true. As b
is false, the first if statement will not be executed. The second if statement uses the logical OR (||
) operator to test if either a
or b
is true. As a
is true, the second if statement will be executed. Finally, we have used the logical NOT (!
) operator to test if a
is false. As a
is true, the third if statement will not be executed.
Range Operators
Range operators are used to create ranges of values. Swift provides two range operators: the closed range operator (...
) and the half-open range operator (..<
).
1let closedRange = 1...5 2let halfOpenRange = 1..<5 3 4for i in closedRange { 5 print(i) 6} 7 8for i in halfOpenRange { 9 print(i) 10}
In the above example, we have created two ranges: 1...5
and 1..<5
. We have then used a for loop to iterate over each value in the ranges and print it to the console.
Ternary Conditional Operator
The ternary conditional operator is a shorthand way of writing an if-else statement. It takes the form of condition ? valueIfTrue : valueIfFalse
. If the condition is true, the first value is returned. If the condition is false, the second value is returned.
1let x = 5 2let y = x > 3 ? "Greater than 3" : "Less than or equal to 3" 3 4print(y) // Output: Greater than 3