Chapter 14 : Operator Precedence

Topics Covered :

1. What are Operator Precedence
2. Examples

# Operator Precedence

You must have learn BODMAS rule while learning Algebra, When ever we have an Algebraic expression Brackets have the greatest precedence followed by Division, Multiplication, Addition and least for Subtraction.

ie. Order of operations

Brackets
Orders(i.e. Powers and roots)
Division
Multiplication
Subtraction

Operator Precedence specifies how tightly it binds two expressions together.

Example :

2 + 2 * 3 = 8, it is not 12 because multiplication (*) operator has a higher precedence than the addition (+) operator.

(2 + 2) * 3 = 12 , because now Brackets (Parentheses) are given more importance.

``````
//
//  main.swift
//  Swift Tutorials 2014-15
//  Operator Precedence Integers in Swift
//
//  Created by Code2care on 15/09/14.
//

var res = (2+2)*4-8/4

//1st Operation Brackets : 2+2 = 4
//2nd Operation Division : 8/4 = 2
//3rd Operation Multiplication 4*4 = 16
//4th Operation Subtraction 16-2 = 14

println("\(res)")

```
```
Output :

14
Program ended with exit code: 0