Swift Operators (Arithmetic, Comparison, Precedence, Logical)

In swift, operators are the symbols that are used to perform operations on given operands like addition, subtraction, division and multiplication based on our requirements.

 

In swift programming language, we have various type of operators available to perform different operations those are

 

  • Arithmetic Operators
  • Assignment Operators
  • Remainder or Modulo Operators
  • Compound Assignment Operators
  • Comparison Operators
  • Logical Operators
  • Range Operators

Arithmetic Operators

In swift arithmetic operators are used to perform mathematical calculations on given operands. 

 

Following are the different types of arithmetic operators available in swift programming language.

 

OperatorDescriptionExample
Addition (+) It performs addition of two operands 1 + 10 = 11
Subtraction(-) It performs subtraction of two operands 1 - 10 = -9
Multiplication (*) It performs multiplication of two operands 30 * 10 = 300
Division (/) It performs division of two operands 30 / 10 = 3

Following is the simple example of using arithmetic operators in a swift programming language.

 

let add = 3 + 65

print(add)

let subtract = 33 - 11

print(subtract)

let multiply = 22 * 4

print(multiply)

let divide = 26 / 2

print(divide)

When we execute above example we will get result like as shown below.

 

68

22

88

13

In swift programming, we can perform string concatenation by using addition (+) operator like as shown below.

 

"Welcome to "+"Tutlane" = "Welcome to Tutlane"

In the above examples we applied operators directly on values now we will see how to use arithmetic operations on variables. Following is the simple example of performing arithmetic operations on variables.

 

let add1 = 9

let add2 = 10

let result = add1 + add2

print(result)

The above swift program will return result like as shown below.

 

19

Assignment Operators

In swift assignment operators are used to assign or update or initialize the values of operands or variables based on our requirements.

 

The following are the different types of assignment operators available in a swift programming language.

 

OperatorDescriptionExample
= It is used to assign right-hand side operands value to left-hand side operand z = x + y
+= It will add right-hand operands to left-hand operand and assign value to left-hand operand x += y (It equals to x = x+y)
-= It will subtract right-hand operands from left-hand operand and assign value to left-hand operand x -= y (It equals to x = x-y)
*= It will multiply left-hand operand with right-hand operands and assign value to left-hand operand x *= y (It equals to x = x*y)
/= It will divide left-hand operand with right-hand operands and assign value to left-hand operand x /= y (It equals to x = x/y)
%= It will calculate modulus left-hand operand with right-hand operands and assign value to left-hand operand x %= y (It equals to x = x%y)

Following is the simple example of using assignment operators in swift programming language.

 

let x = 10

var y = 100

x = y

print(x)

 

let a = 10

let b = 3

a += b

print(a)

 

let m = 20

let n = 10

m *= n

print(m)

 

let h = 26

let i =  2

h /= i

print(h)

When we execute the above example we will get result like as shown below.

 

100

13

200

13

Remainder or Modulo Operator

Generally, the remainder or modulo operator (x % y) will return value that is leftover or remainder after dividing the x with y.

 

In Swift programming language we will define modulo operator as “%” and its operation like as shown below.

 

let modulo = 27 % 10

print(modulo)

 

let min = -27 % 10

print(min)

When we execute the above swift program it will return a result like as shown below.

 

7

-7

Compound Assignment Operators

The swift programming language provides an operator called compound assignment operator to combine assignment (=) with another operation like addition (+=) or subtraction (-=).

 

Following is the syntax of using compound assignment operator in swift programming language.

 

a += 1 (It equals to a = a+1)

b -= 1 (It equals to b = b-1)

Following is the simple example of using compound assignment operator in swift programming language.

 

var c = 0

c += 1

print(c)

 

c -= 1

print(c)

 

c += 5

print(c)

 

c -= 2

print(c)

When we run above program in swift playground we will get result like as shown below.

 

1

0

5

3

Comparison Operators

In swift programming language, comparison operators are useful to compare operand values based on our requirements.

 

Following are the comparison operators available in a swift programming language.

 

OperatorDescriptionExample
Equal to (==) It is used compare both operands are equal or not. If both are equal then the condition will return true x == y
Not Equal to (!=) It is used to compare both operands are equal or not. If both are not equal then the condition will return true x != y
Greater than (>) It is used to check if left-hand operand greater than right-hand operand or not. If left-hand operator is greater, then the condition will return true x > y
Less than (<) It is used to check if left-hand operand less than right-hand operand or not. If left-hand operator lesser, then the condition will return true x < y
Greater than or Equal to (>=) It is used to check if left-hand operand greater than or equal to the value of right-hand operand or not. If yes then the condition will return true x >= y
Less than or Equal to (<=) It is used to check if left-hand operand is less than or equal to the value of right-hand operand or not. If yes then the condition will return true x <= y

Following is the simple example of using comparison operators in swift programming language.

 

let e = (3 == 4)

print(e)

 

let ne = (3 != 4)

print(ne)

 

let gt = (3 > 4)

print(gt)

 

let lt = (3 < 4)

print(lt)

 

let lte = (3 <= 4)

print(lte)

 

let gte = (3 >= 4)

print(gte)

When we run above swift program in playground we will get result like as shown below.

 

False

True

False

True

True

False

Logical Operators

In swift programming language, logical operators are useful to combine or compare Boolean logic values.

 

Following are the different types of logical operators available in a swift programming language.

 

  • Logical NOT (!x)
  • Logical AND (x && y)
  • Logical OR (x || y)

Logical NOT (!x) Operator 

In swift Logical NOT operator is useful to invert the value. In case our value is TRUE then it will become FALSE otherwise FALSE become TRUE.

 

Following is the simple example of a logical not operator in swift programming language.

 

let x = true

if(!x)

print("Done")

else

print("Failed")

When we run above swift program we will get result like as shown below.

 

Failed

Logical AND (x && y) Operator

In swift Logical AND operator will check if all defined conditions are true or not. 

 

Following is the simple example of using Logical AND operator in swift programming language.

 

if (2 < 3 && 3>7)

{

print("Condition True")

}

else

{

print("Condition Failed")

}

In above Logical AND operator example first condition (2<3) is true but second condition (3>7) failed because of that it will return else condition value. 

 

Following is the result of Logical AND operator example.

 

Condition Failed

Logical OR (x || y) Operator

In swift Logical OR operator is used to check if any one of the defined condition is TRUE or not.

 

Following is the simple example of a Logical OR operator in swift to check the defined conditions TRUE or not.

 

if (2 < 3 || 3>7)

{

print("Condition True")

}

else

{

print("Condition Failed")

}

In above example first condition is TRUE but second condition is FALSE even though Logical OR function will return TRUE either of one condition become TRUE.

 

Following is the result of a Logical OR operator example.

 

Condition True

Range Operators

In swift range operators are useful to define a range (x…y) that runs from x to y but the value of x always must be less than y.

 

Generally we use Range operator in swift to calculate number of items in list or get the items by looping through the list.

 

Suppose we have list of students in list but we don’t know how many items are there in that list that time Range operator will help us to loop through that list to count number of items or get items in list. 

 

This Range operator is helpful when we using list-objects like arrays, Dictionaries and etc.

 

let studets = ["Suresh Dasari", "Khawar Islam"]

let count = studets.count

for i in 0count {

print("Student \(i + 1) is called \(studets[i])")

}

If you observe above swift example we are looping based on the range value to get elements from defined list.

 

The swift playground will return the result like as shown below

 

Student 1 is Suresh Dasari

Student 2 is Khawar Islam

This is how we can use different types of operators in a swift programming language based on our requirements.