← Previous | 🏠 Homepage | Next Chapter → |
Hierarchy of Operations (Operator Precedence)
When you write a math expression in C with many operators (like +
, *
, /
), the computer needs to know which part to solve first.
This is called operator precedence — it tells the computer the correct order to do the operations.
Priority of Operators
Examples:
i = 2 * 3 / 4 + 4 / 4 + 8 - 2 + 5 / 8;
Steps:
-
2 * 3 = 6
, then6 / 4 = 1
(integer division) -
4 / 4 = 1
-
5 / 8 = 0
-
Final:
1 + 1 + 8 - 2 + 0 = 8
Integer division cuts off the decimal part!
So5 / 8 = 0
not0.625
Associativity of Operators
-
Left to Right: e.g.
3 / 2 * 5
→ first/
, then*
-
Right to Left: e.g.
a = b = 3
→ firstb = 3
, thena = b
Algebra vs C Expressions
Algebraic Expression | C Expression |
---|---|
a × b − c × d | a * b - c * d |
(m + n)(a + b) | (m + n) * (a + b) |
3x² + 2x + 5 | 3 * x * x + 2 * x + 5 |
(a + b + c) / (d + e) | (a + b + c) / (d + e) |
Chapter 2 : Making Decisions in 'C'
4 Main Types:
-
Sequence – Run in order (top to bottom).
-
Decision – Choose one path (like
if-else
). -
Looping – Repeat steps (like
for
,while
). -
Case – Choose from many options (
switch
).
The if
Statement in C
Basic Form:
if (condition)
statement;
-
If the condition is true, the statement runs.
-
If the condition is false, it is skipped.
Relational Operators (used in conditions):
Example:
int num;
scanf("%d", &num);
if (num <= 10)
printf("You're an obedient servant!");
Real-Life Example: Discount Program
If you buy more than 1000 items, you get a 10% discount.
int qty, dis = 0;
float rate, tot;
scanf("%d %f", &qty, &rate);
if (qty > 1000)
dis = 10;
tot = (qty * rate) - (qty * rate * dis / 100);
printf("Total = Rs. %f", tot);
Always initialize variables like dis = 0
, or it may hold garbage.
Fun Fact: Non-zero = TRUE
if (3 + 2 % 5) // true (non-zero)
if (a = 10) // true (assignment done, value is 10)
if (-5) // true (non-zero)
-
0 is false
-
Any non-zero (positive or negative) is true
Multiple Statements in if
Use {}
to group multiple lines:
if (years > 3) {
bonus = 2500;
printf("Bonus = %d", bonus);
}
If you skip the braces, only the next line is considered part of the if
.
The if-else
Statement
Example: Salary Calculator
float bs, hra, da, gs;
scanf("%f", &bs);
if (bs < 1500) {
hra = bs * 0.10;
da = bs * 0.90;
} else {
hra = 500;
da = bs * 0.98;
}
gs = bs + hra + da;
printf("Gross Salary = Rs. %f", gs);
Nested if-else
in C (if inside if!)
You can put one if-else
inside another. This is called nesting.
Example:
int i;
printf("Enter 1 or 2: ");
scanf("%d", &i);
if (i == 1)
printf("You would go to heaven!");
else {
if (i == 2)
printf("Hell was created with you in mind");
else
printf("How about mother earth!");
}
What’s Happening?
Else, if i == 2
→ it prints “Hell was created with you in mind”
Else → it prints “How about mother earth!”
This is a nested if-else
: an if-else
inside another else
.
Why Indentation Matters:
This makes the code easy to read and understand.
Forms of if
in C
C gives you many ways to write if
:
- Basic if
:
if (condition)
statement;
- if
with a block:
if (condition) {
statement1;
statement2;
}
- if-else
:
if (condition)
do_this;
else
do_that;
- if-else
with blocks:
if (condition) {
do_this;
and_this;
} else {
do_that;
and_that;
}
- Nested inside else
:
if (condition)
do_this;
else {
if (condition)
do_that;
else {
do_something_else;
maybe_more;
}
}
- Nested inside if
:
if (condition) {
if (condition)
do_this;
else {
do_that;
and_that;
}
} else
do_else;
No comments:
Post a Comment