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.

OperatorExampleDescription
+3 + 4Addition
-7 - 2Subtraction
*5 * 6Multiplication
/9 / 3Division
%8 % 3Modulo (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.

OperatorExampleDescription
==3 == 3Equal to
!=4 != 6Not equal to
>5 > 3Greater than
<2 < 7Less than
>=8 >= 6Greater than or equal to
<=2 <= 2Less 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.

OperatorExampleDescription
&&true && falseLogical AND
||true || falseLogical OR
!!trueLogical 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:

OperatorExampleDescription
=x = 3Assigns the value 3 to x
+=x += 2Adds 2 to the value of x
-=x -= 4Subtracts 4 from the value of x
*=x *= 5Multiplies the value of x by 5
/=x /= 2Divides the value of x by 2
%=x %= 3Takes 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).

OperatorExampleDescription
&&true && truewill return true if both are true
||true || truewill return true if atleast one is true
!!falsewill 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