Introduction
In Data Structures and Algorithms (DSA), precedence refers to the order in which operations are performed in an expression. Some operations have higher priority than others, meaning they are executed before lower-priority operations.
For example, in mathematics:
5 + 3 × 2 = ?
- If we add first:
(5 + 3) × 2 = 8 × 2 = 16 ❌ (Wrong)
- If we multiply first:
5 + (3 × 2) = 5 + 6 = 11 ✅ (Correct)
Since multiplication (×
) has higher precedence than addition (+
), we must multiply first.
1️⃣ Why is Precedence Important?
Precedence ensures that expressions are evaluated correctly in programming. Without precedence rules, the computer would not know which operation to perform first, leading to incorrect results.
For example, in C++:
int result = 10 + 5 * 2;
- If addition happens first: (10 + 5) * 2 = 30 ❌ (Wrong)
- If multiplication happens first: 10 + (5 * 2) = 10 + 10 = 20 ✅ (Correct)
Since multiplication has higher precedence than addition, it is performed first.
2️⃣ Operator Precedence Table
Different operations have different precedence levels. Operators with higher precedence are executed before lower-precedence operators.
Operator | Example | Precedence (Highest to Lowest) |
---|---|---|
() |
(3 + 2) * 4 |
1 (Highest) |
++ , -- |
a++ , b-- |
2 |
* , / , % |
4 * 2 , 9 / 3 |
3 |
+ , - |
6 + 3 , 10 - 5 |
4 |
<, >, <=, >= |
a > b , x <= y |
5 |
==, != |
a == b , x != y |
6 |
&& |
a && b |
7 |
` | ` | |
=, +=, -= |
x = 5 , y += 3 |
9 (Lowest) |
📌 Tip: Operators at the top of the table have higher precedence than those at the bottom.
3️⃣ How Precedence Works in Coding
Let's take an example in C++:
#include <iostream>
using namespace std;
int main() {
int result = 10 + 5 * 2;
cout << "Result: " << result;
return 0;
}
Step-by-Step Execution:
- Multiplication (
*
) has higher precedence than addition (+
). - First,
5 * 2
is calculated →10
. - Then,
10 + 10
is calculated →20
.
Output:
Result: 20
This shows how precedence ensures the correct order of execution.
4️⃣ Associativity in Precedence
Sometimes, two operators have the same precedence. In such cases, associativity determines the order of execution.
4.1 Left to Right Associativity
If two operators have the same precedence, they are solved from left to right.
Example:
int x = 10 - 2 - 3;
Step-by-step:
(10 - 2) = 8
8 - 3 = 5
Final answer:5
4.2 Right to Left Associativity
Some operators, like assignment (=
), are evaluated from right to left.
Example:
int a, b, c;
a = b = c = 10;
Step-by-step:
- First,
c = 10
- Then,
b = 10
- Finally,
a = 10
Now, all three variables have the value 10
.
5️⃣ Precedence in Stacks (Expression Evaluation)
Precedence is useful when working with stacks to evaluate expressions in different forms:
- Infix Notation →
A + B * C
(Normal way we write expressions) - Postfix Notation (Reverse Polish Notation - RPN) →
A B C * +
Using stacks, computers convert infix expressions into postfix form for easier evaluation.
Example: Converting Infix to Postfix
Expression: A + B * C
- Since
*
has higher precedence,B * C
is calculated first. - The expression is converted into postfix form:
A B C * +
- This makes it easier for computers to process.
📌 Fun Fact: Programming languages like Python and Java follow the same precedence rules to evaluate expressions.
6️⃣ Key Takeaways
✔ Precedence decides which operation is performed first in an expression.
✔ Some operators have higher precedence than others (e.g., *
before +
).
✔ Associativity decides the order when operators have the same precedence (e.g., -
is left to right, =
is right to left).
✔ Understanding precedence is important in coding to avoid logical errors.
✔ Stacks are used to evaluate expressions based on precedence (e.g., converting infix to postfix).
7️⃣ Final Tip
💡 When in doubt, use parentheses ()
to control precedence manually!
Example:
int result = (10 + 5) * 2; // Now addition happens first!
This ensures your calculations are correct, just like in math! 🎯
Would you like more examples or explanations? 😊