EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Swift Tutorial Swift Operators
Secondary Sidebar
Swift Tutorial
  • Basics
    • How To Install Swift?
    • Swift Version
    • Swift Package Manager
    • Swift Operators
    • Swift Loop
    • Swift For Loop
    • Swift while loop
    • Swift do-while
    • Swift array
    • Swift Queue
    • Swift Dictionary
    • Swift forms
    • Swift sets
    • Swift map
    • Swift int to string
    • Swift Interview Questions
    • Swift extension
    • Swift guard
    • Swift enum
    • Swift zip
    • Swift?hashable
    • MVVM Swift

Swift Operators

By Priya PedamkarPriya Pedamkar

Swift Operators

Introduction to Swift Operators

An operator is a symbol that helps to perform various logical and mathematical computations. Swift supports most of the standard C operators.

They are classified into three types:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  1. Unary operators: These operators operate on a single operand only. For example, increment operator.
  2. Binary operators: These operators operate on two operands. For example, the addition operator.
  3. Ternary operators: These operators operate on three operands. For example, a ? b: c.

Swift Operators

Different operators present in swift are as follows:

  • Arithmetic Operator
  • Logical Operator
  • Assignment Operator
  • Comparison Operator
  • Bitwise Operator
  • Range Operator
  • Miscellaneous Operator

Now let us see each type of operator in detail:

1. Swift Arithmetic Operators

These operators are used to perform mathematical calculations on the operands.

Operator

Symbol Explanation

Format

Addition

+

Adds given two operands

x + y

Subtraction

–

Subtracts the right operand from the left one.

x – y

Multiplication

*

Multiplies two operands

x * y

 

Division

/

Divides the numerator by the denominator

x / y

 

Modulus

%

Returns the remainder after performing division

x % y

Example:

print(5  +  2)
print(5  -  2)
print(5  *  2)
print(5  /  2)
print(5  %  2)

Output:

7

3

10

2

1

2. Swift Logical Operator

These operators return Boolean values taking Boolean values as input.

Operator Symbol Explanation Format
Logical AND && Returns true if all expressions are true; else return false x && y
Logical OR || Returns false if all expressions are false; else return true x || y
Logical NOT ! Inverses the input, i.e. return true for false and vice versa !x

Example:

print(true  &&  true)
print(true  &&  false)
print(true  ||  false)
print(false  ||  false)
print(! false)

Output:

true

false

true

false

true

3. Swift Assignment Operator

These operators are used to assign values to a variable.

Operator Symbol Explanation Format
Assignment = Assigns a value of right operand to the left operand x = y
Addition += Adds two operands and then assigns a value to the left operand x += y
Subtraction -= Subtracts right operand from left operand and then assigns the value to left operand x -= y
Multiplication *= Multiplies two operands and then assigns a value to the left operand x *= y
Division /= Divides the numerator by denominator and then assigns a value to the left operand x /= y
Modulus %= Returns remainder after division and then assigns a value to left operand x %= y
Bitwise AND &= Compares the binary value of two operands, return 1 if both operands are 1 else return 0 and assign a value to the left operand x &= y
Bitwise OR |= Compares the binary value of two operands, return 0 if both operands are 0 else return 1 and assign the value to the left operand x |= y
Bitwise XOR ^= Compares the binary value of two operands, return 0 if both operands are same else return 1 and assign a value to the left operand x ^= y
Left Shift <<= Shifts the bits towards the left and assign the result to the left operand x <<= 2
Right Shift >>= Shifts the bits towards the right and assign the result to the left operand x >>= 2

Example:

let a = 5
print (a)
var x = 6
print(x += 3)
print(x -= 3)
print(x *= 3)
print(x /= 3)
print(x %= 3)
print(x &= 3)
print(x |= 3)
print(x ^= 3)
print(x <<= 2)
print(x >>= 2)

Output:

5

9

3

18

2

0

2

7

5

8

1

4. Swift Comparison Operator

These operators help to compare two values and return Boolean values as output.

Operator Symbol Explanation Format
Equal to == Returns true if both operands are equal else return false x == y
Not Equal to != Returns true if both operands are not equal; else return false x != y
Greater than > Returns true if the left operand is greater than right; else return false x > y
Less than < Returns true if the left operand is smaller than right; else return false x < y
Greater than or equal to >= Returns true if the left operand is greater than or equal to the right; else return false x >= y
Less than or equal to <= Returns true if the left operand is smaller than or equal to the right; else return false x <= y

Example:

print(5  ==  2)
print(5  !=  2)
print(5  >  2)
print(5  <  2)
print (5 >= 5)
print (5  <=  2)

Output:

false

true

true

true

true

false

5. Swift Bitwise Operator

Operator Symbol Explanation Format
Binary AND & Check the operands bitwise and return 1 if both bits are 1; else return 0 x & y
Binary OR | Check the operands bitwise and return 0 if both bits are 0; else return 1 x | y
Binary XOR ^ Check the operands bitwise and return 0 if both bits are the same; else return 1 x ^ y
Binary NOT ~ Returns ones complement, i.e. changes 1 to 0 and vice versa

~x

 

Binary Left Shift << Bits of the left operand is shifted left side by the number of bits mentioned by the right operand x << 4
Binary Right Shift >> Bits of the left operand is shifted right side by the number of bits mentioned by the right operand x >> 4

Example:

var a = 8
var b = 7
print(a & b)
print(a | b)
print(a ^ b)
print(~ b)
print(a << 2)
print(a >> 2)

Output:

0

15

15

8

0

2

6. Swift Range Operators

These operators are used as shortcuts to express the range of values.

Operator Symbol Explanation Format
Closed Range (a…b) It defines a range from a to b, both included 1…5
Half – Open Range (a..<b) It defines the range from a to b; an included while b excluded 1..<5
One-sided Range

a…

..a

It defines the range from a to end of elements or from start to a

1…

…2

Example:

for i in 1...4 {
print(i)}
for j in 1. . <4 {
print(j) }
let range = ..<4
print(range.contains(2))

Output:

1

2

3

4

1

2

3

true

7. Swift Miscellaneous Operators

Operator Symbol Explanation Format
Unary Plus + This toggles the sign of the numeric value to plus +5
Unary Minus – This toggles the sign of the numeric value to minus. -6
Ternary condition ? : Used to check a condition and then give output accordingly Condition? a: b

Example:

var a = -3
print(+a)
print(-a)
let b = (6==6) ? “True”: “false”
print(b)

Output:

-3

3

True

Recommended Articles

This has been a guide to Swift Operator. Here we have discussed the various type of Swift operators with examples. You can also go through our other suggested articles to learn more –

  1. Swift vs Objective C
  2. Swift and Go
  3. C Operators
  4. MySQL Operators
Popular Course in this category
Become a Complete iOS Developer (30 Courses, 31 Projects)
  30 Online Courses |  31 Hands-on Projects |  157+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Swift Training (10 Courses, 11+ Projects)4.9
Windows Forms Training (4 Courses, 3+ Projects)4.8
Kali Linux Training (3 Courses, 3+ Projects)4.7
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more