Swift Operators (original) (raw)

Last Updated : 11 Oct, 2022

Swift is a general-purpose, multi-paradigm, and compiled programming language which is developed by Apple Inc. In Swift, an operator is a symbol that tells the compiler to perform the manipulations in the given expression. Just like other programming languages, the working of operators in Swift is also the same. We can perform multiple operations using operators in our programs there are multiple operators available in Swift. In this article, we will learn all types of operators which are present in the Swift programming language.

Arithmetic Operators

// Swift program to illustrate the // use of arithmetic changes import Swift

// Creating two variables a and b var a = 5 var b = 4

// Adding two number var sum = a + b print("Addition:", sum)

// Subtracting two number var sub = a - b print("Subtraction:", sub)

// Dividing two number var div = a / b print("Divide:", div)

// Multiplying two number var mul = a * b print("Multiply:", mul)

// Finding Modulus var mod = a % b print("Modulus:", mod)

`

Output:

Addition: 9 Subtraction: 1 Divide: 1 Multiply: 20 Modulus: 1

Assignment Operators

// Swift program to illustrate the // use of assignment operators import Swift

// Creating two variables a & b var a = 5 var b = 1

// Plus equal to a += b print("Result of += operator: ", a)

// Minus equal to a -= b print("Result of -= operator: ", a)

// Divide equal to a /= b print("Result of /= operator: ", a)

// Multiply equal to a *= b print("Result of *= operator: ", a)

// Modulus equal to a %= b print("Result of %= operator: ", a)

`

Output:

6 5 5 5 0

Bitwise Operators

// Swift program to illustrate the // use of bitwise operators import Swift

// Creating variables a and b var a = 2 var b = 1

// Left shift operator print("Bitwise left shift operator: ", a << b)

// Right shift operator print("Bitwise right shift operator: ", a >> b)

// AND operator print("AND operator: ", a & b)

// OR operator print("OR operator: ", a | b)

// XOR operator print("XOR operator: ", a ^ b)

// Tilde operator print("Tilde operator: ", ~a)

`

Output:

4 1 0 3 3 -3

Comparison Operators

// Swift program to illustrate the // use of Comparison operators import Swift

// Creating two variables a & b var a = 2 var b = 1

// Less than operator print(a < b)

// Greater than operator print(a > b)

// Equal to operator print(a == b)

// Not equal to operator print(a != b)

// Less than equal to operator print(a <= b)

// Greater than equal to operator print(a >= b)

`

Output:

false true false true false true

Logical Operators

// Swift program to illustrate the // use of Logical Operators import Swift

// Creating variables a, b & c var a = 1 var b = 2 var c = 3

// Logical AND operator if (a < b && b < c) { print("Both condition is true") }

// Logical OR operator if (a < b || a > c) { print("Only one condition is true") }

// Logical NOT operator if (true && !false) { print("Logic reversed") }

`

Output:

Both condition is true Only one condition is true Logic reversed

Misc Operators

// Swift program to illustrate the use of // Ternary and Nil-coalescing Operator import Swift

// Creating variables a, b var a = 1 var b = 2

// Ternary operator var c = a < b ? 3 : 4 print(c)

let name: String? = nil

// Nil-Coalescing Operator let newName = name ?? "GFG"

// If we don't pass GFG then the // default value will be nil in newName print(newName)

`

Output:

3 GFG

Range Operators

// Swift program to illustrate the use of // range Operators import Swift

// Closed Range Operator - It will print up to 3 for i in 1...3 { print("closed = (i)") }

// Half open Range Operator - It will only print up to 2 for i in 1..<3 { print("half = (i)") }

// One sided Range Operator - It will print all // values in array from index 0 let values = [ 1, 2, 3, 4 ] for i in values[0...] { print("One sided = (i)") }

`

Output:

closed = 1 closed = 2 closed = 3 half = 1 half = 2 One sided = 1 One sided = 2 One sided = 3 One sided = 4

Operator Precedence

In swift operator, precedence is used to find the grouping terms in the given expression. Also, used to evaluate the expression. For example, y = 3 + 4 * 2, here * has the highest precedence so first, we solve 4 *2 and then we add the result with 3. Precedence will decrease while we visit the bottom of the table given below.

Operator Name Operators
Bitwise Shift <<, >>
Multiplicative %, *, /
Additive |, -, +, -, ^
Range Operators ..<, ...
Nil-Coalescing Operator ??
Comparison Operators <, >, <=. >=, ==, !=
Logical Operators &&, |
Ternary Operator ? :
Assignment Operators |=, %=, /=, *=, >>=, <<=, ^=, +=, -=