20.6.25

20250620 17:00 Chapter 2 : Making Decisions in 'C'

                        

← 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


Priority         Operators        Meaning
1st* / %Multiply, Divide, Modulus
2nd+ -Add, Subtract
3rd=Assignment

Examples:

i = 2 * 3 / 4 + 4 / 4 + 8 - 2 + 5 / 8;

Steps:

  • 2 * 3 = 6, then 6 / 4 = 1 (integer division)

  • 4 / 4 = 1

  • 5 / 8 = 0

  • Final: 1 + 1 + 8 - 2 + 0 = 8

Integer division cuts off the decimal part!
So 5 / 8 = 0 not 0.625

Associativity of Operators

When two operators have equal priority, the associativity decides which one goes first.
  • Left to Right: e.g. 3 / 2 * 5 → first /, then *

  • Right to Left: e.g. a = b = 3 → first b = 3, then a = 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'


Control Instructions in C

Control instructions guide the program's flow — how and when things happen.

4 Main Types:

  1. Sequence – Run in order (top to bottom).

  2. Decision – Choose one path (like if-else).

  3. Looping – Repeat steps (like for, while).

  4. Case – Choose from many options (switch).

The if Statement in C

C lets you make decisions in your program using the if statement.

 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):

Note: = is for assignment, == is for comparison.

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


Want to do one thing if true and another if false? Use if-else.

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?


If i == 1 → it prints “You would go to heaven!”
  • 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:


Every time you go deeper with ifs, indent (move the line to the right).

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

rating System

Loading...

Understanding Arrays in C

The Bookshelf Analogy & Book-Author Example Arrays are one of the most essential concepts in C programming. They let you store multiple ...