Table of contents
1.
Introduction
2.
Types of Operators
2.1.
Arithmetic Operators
2.2.
Assignment Operators
2.3.
Comparison Operators
2.4.
Logical Operators
2.5.
Bitwise Operators
2.6.
Range Operator
3.
Frequently Asked Questions
3.1.
What is the use of the let keyword in swift?
3.2.
What is the use of the var keyword in swift?
3.3.
What is the difference between var and let keywords?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Operators in Swift

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this article, we will be learning about the operators in the swift language. An operator is a special symbol that performs specific mathematical and logical operations on the variables (operands). Operators in swift are of three types based on the number of operands:

  1. Unary: It uses one operand. Unary operators work with only one target (such as -a). Unary prefix operators (such as !b) and unary postfix operators (such as c!) appear immediately before and after their targets, respectively.
  2. Binary: It consists of two operands. Binary operators (such as 2 + 3) operate on two targets and are infix because they appear between the two targets.
  3. Ternary: Ternary operators are focused on three distinct targets. Swift contains only one ternary operator, the ternary conditional operator (a? b: c), similar to C.

Now let's discuss the type of operators based on their operations.

Know more about Unary operator overloading in c++ in detail here.

Types of Operators

In this section, we will discuss different types of operators:

Arithmetic Operators

In swift, we use arithmetic operations to perform several mathematical operations like addition, subtraction, multiplication, division, etc.

Arithmetic operators are:

Example: In this example, we will use different operators.

Code:

var a = 7
var b = 3

// Addition
print (a + b)  

// Subtraction
print (a - b) 

// Multiplication
print (a * b)  

// Division
print(a / b)

// Modulo
print(a % b)


Output:

10
4
21
2
1

Assignment Operators

In swift, we use the assignment operator to assign the value to the variables. Types of assignment operators are:

Example: In this example, we will discuss all the assignment operators.

Code:

// Assign 2 to x
var x = 2

// Assign 8 to y
var y = 8 

// Using addition assignment.
x += y
print(x) //x = 10

// Using subraction assignment.
x -= 2
print(x) //x = 8

// Using multiplication assignment.
y *= 2
print(y) //y = 16

// Using division assignment.
x /= 2
print(x) //x = 4

// Using modulo assignment.
x %= 3
print(x) //x = 1


Output:

10
8
16
4
1

Comparison Operators

In swift, we use a comparison operator to compare the two variables. Types of comparison operators are:

Example: In this example, we will discuss all the comparison operators.

Code:

// Is equal to
print(2 == 2)

// Not equal to
print(2 != 2)

// Greater than
print(4 > 1)

// Smaller than
print(3 < 1)

// Greater than or equal to
print(10 >= 10)

// Smaller than or equal to
print(12 <= 20)


Output:

true
false
true
false
true
true

Logical Operators

In swift, we use a logical operator to check if an expression is true or false. It is also used in decision flow control.

Example: In this example, we will discuss all the logical operators.

Code:

// Logical Not
print(!true)

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

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


Output:

false
true
false
true
false

Bitwise Operators

In swift bitwise operator is used to perform operations on the bits. Types of bitwise operators are:

Example: In this example, we will discuss all the logical operators.

Code:

// Binary XOR
print(2 ^ 3)

// Binary AND
print(10 & 10)

// Binary OR
print(4 | 5)

// Left Shift
print(10<<2)

// Right Shift
print(12>>2)


Output:

1
10
5
40
3

Range Operator

In swift, we use the range operator to show the range of values. Types of range operators are:

Example: In this example, we will discuss all the logical operators.

Code:

// Using a...b
print("Using a...b operator:")
for x in 1...5{
    print(x)
}


// Using a..<b
print("Using a..<b operator:")
for x in 1..<5{
    print(x)
}


// Using a... or ...a or ..<a
print("Using a... or ...a or ..<a operator:")
let range1 = ..<3
print(range1.contains(-3))
let range2 = 4...
print(range2.contains(10))


Output:

Using a...b operator:
1
2
3
4
5
Using a..<b operator:
1
2
3
4
Using a... or ...a or ..<a operator:
true
true

 

Read about Bitwise Operators in C here.

Frequently Asked Questions

What is the use of the let keyword in swift?

In Swift, we use the let keyword to construct immutable variables. An immutable variable is a constant that can only be initialized once.

 

What is the use of the var keyword in swift?

To declare a variable in Swift, we utilize the var keyword. Variables are used in Swift to store and refer to values by their names. Before they can be utilized, variables must be declared.  

 

What is the difference between var and let keywords?

After a var variable has been initialized, it can be altered. On the other hand, a let variable cannot be modified after it has been created. Attempting to reassign, reinitialize, or mutate a Swift let variable will result in a compiler error, similar to const variable in other languages.

Conclusion

In this article, we have extensively discussed the Operators in swift. We discussed the different types of literal along with their examples.

We hope this blog has helped you enhance your knowledge of the Operators. Check out the awesome content on the Coding Ninjas Website, Swift Environment setupSwift setsSwift dictionarySwift Online ResourcesCoding Ninjas Studio ProblemsCoding Ninjas Studio Interview BundleCoding Ninjas Studio Interview ExperiencesCoding Ninjas CoursesCoding Ninjas Studio Contests, and Coding Ninjas Studio Test SeriesDo upvote our blog to help other ninjas grow. 

Happy Coding!

Live masterclass