Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Swift – Operators

  • Last Updated : 02 Feb, 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

  • + (Addition): This operator is used to add two variables.
  • – (Subtraction): This operator is used to subtract the second variable from the first.
  • / (Division): This operator is used to divide the first variable from the second.
  • * (Multiplication): This operator is used to multiply the first variable with the second.
  • % (Modulus): This operator gives the remainder after division first from second.

Swift




// 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

  • = (Equal): This operator is used to assign values from the right side variable to the left side variable.
  • += (Plus equal to): This operator is used to add the right variable to the left variable and assign the result to the left variable.
  • -= (Minus equal to): This operator is used to subtract the right variable from the left variable and assign the result to the left variable.
  • /= (Divide equal to): This operator is used to divide the left variable with the left variable and assigns the result to the left variable.
  • *= (Multiply equal to): This operator is used to multiply the right variable with the left variable and assign the result to the left variable.
  • %= (Modulus equal to): This operator is used to take modulus using two variables and assigns the result to the left variable

Swift




// 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

  • << (Left Shift): The left variable value is moved left by the number of bits mentioned by the right variable.
  • >> (Right Shift): The left variable value is moved right by the number of bits mentioned by the right variable.
  • & (Binary AND): This operator copies bits to the result if the bits are set in both variables.
  • | (Binary OR): This operator copies bits to the result if the bits are set in any variables.
  • ^ (Binary XOR): This operator copies bits to the result if the bits are set on only one variable.
  • ~ (Tilde): This operator gives binary one’s complement of the variable

Swift




// 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 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

  • < (Less than): If the value of the left variable is less than the value of the right variable then the condition becomes true.
  • > (Greater than): If the value of the left variable is greater than the value of the right variable then the condition becomes true.
  • == (Equal to equal to): If the value of both variables is equal then the condition becomes true.
  • <= (Less than Equal to): If the value of the left variable is less than or equal to the value of the right variable then the condition becomes true.
  • >= (Greater than Equal to): If the value of the left variable is greater than or equal to the value of the right variable then the condition becomes true.
  • != (Not equal to): If the value of both variables is not equal then the condition becomes true.

Swift




// 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

  • && (Logical AND): If both the side conditions are true, then the condition becomes true.
  • || (Logical OR): If any side condition is true, then the condition becomes true.
  • ! (Logical NOT): This operator is to reverse the logical state.

Swift




// 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

  • Cond ? A : B (Ternary Operator): If the condition becomes true, then value A will be assigned otherwise the value B will be assigned.
  • (A ?? B) (Nil-coalescing Operator): If the user didn’t pass any value to the variable then the default value will be nil.
  • ++ (Increment): This operator has been used to increase the value of the variable by 1.
  • — (Decrement): This operator is used to decrease the value of the variable by 1.

Swift




// 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

  • (a…b) (Closed Range Operator) — It runs from a to b including the values a and b. It is used in mostly used in for-in loops.
  • (a..<b) (Half-Open Range Operator) — It runs from a to b, but it doesn’t include the value of b. It is used in mostly used in for-in loops.
  • [a…] (One-Sided Range Operator) — It will run for ranges that continue as far as possible in one direction.

Swift




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

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!