Swift Operators

Hi everyone do you know about Swift Operators. With this article, you can clearly see everything you have in doubt.

Swift Operators

Swift Operators

An operator is a symbol that tells a compiler to perform specific mathematical or logical manipulations.

Types of Operators

  • Arithmetic operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators
  • Bitwise Operators

1. Swift Arithmetic Operators

var sub = 10 - 5 // 5
OperatorOperationExample
+Addition5 + 2 = 7
-Subtraction4 - 2 = 2
*Multiplication2 * 3 = 6
/Division4 / 2 = 2
%Modulo5 % 2 = 1

Example 1: Arithmetic Operators in Swift

var a = 7
var b = 2

// addition
print (a + b)  

// subtraction
print (a - b) 

// multiplication
print (a * b)  

Output

9
5
14

In the above example, we have used

  • + to add a and b
  • - to subtract b from a
  • * to multiply a and b

/ Division Operator

// returns quotient only
7 / 2 = 3
// perform division
7.0 / 3.0 = 3.5

% Modulo Operator

print(9 % 4)     // 1

2. Swift Assignment Operators

// assign 5 to x 
var x = 5
OperatorNameExample
=Assignment Operatora = 7
+=Addition Assignmenta += 1 // a = a + 1
-=Subtraction Assignmenta -= 3 // a = a - 3
*=Multiplication Assignmenta *= 4 // a = a * 4
/=Division Assignmenta /= 3 // a = a / 3
%=Remainder Assignmenta %= 10 // a = a % 5

Example 2: Assignment Operators

// assign 10 to a
var a = 10

// assign 5 to b
var b = 5 

// assign the sum of a and b to a
a += b      // a = a + b

print(a)

Output

15

3. Swift Comparison Operators

var a = 5, b =2
print (a > b)      // true
OperatorMeaningExample
==Is Equal To3 == 5 gives us false
!=Not Equal To3 != 5 gives us true
>Greater Than3 > 5 gives us false
<Less Than3 < 5 gives us true
>=Greater Than or Equal To3 >= 5 give us false
<=Less Than or Equal To3 <= 5 gives us true

Example 3: Comparison Operators

var a = 5, b = 2

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

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

// greater than operator
print(a > b)

// less than operator
print(a < b)

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

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

Output

false
true
true
false
true
false

4. Swift Logical Operators

var a = 5, b = 6
print((a > 2) && (b >= 6))    // true
OperatorExampleMeaning
&&a && bLogical AND:
true only if both the operands are true
||a || bLogical OR:
true if at least one of the operands is true
!!aLogical NOT:
true if the operand is false and vice-versa.

Example 4: Logical Operators

// logical AND
print(true && true)      // true
print(true && false)     // false

// logical OR
print(true || false)      // true

// logical NOT
print(!true)                  // false

Output

true
false
true
false

5. Swift Bitwise Operators

OperatorDescription
&Binary AND
|Binary OR
^Binary XOR
~Binary One’s Complement
<<Binary Shift Left
>>Binary Shift Right

Read Also: Data Types in Swift

 Final Words

Through this article, we have come to know clearly that Swift Operators. If you have any doubts please leave a comment via the comment box.

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Deekshi. Here, I post about programming to help developers.

Share on:

Leave a Comment