MCS-201 · Unit 3

Decision and Loop Control Statements

Section 2 of 7

Block 1 · Unit 3Decision and Loop Control Statements8 min · Section 2 of 7

3.2Nested if and the else-if Ladder

Covers IGNOU MCS-201 §3.2.1

Many decisions have more than two outcomes: fare discounts by age, grades by marks, tax by income slab. C handles these by placing if statements inside one another (nesting) or by chaining them into an else if ladder.

Nested if–else

A nested if is an if or if–else statement placed inside the body of another if or else.

Syntax
if (condition1) {
    if (condition2) {
        block A      /* condition1 true, condition2 true  */
    } else {
        block B      /* condition1 true, condition2 false */
    }
} else {
    block C          /* condition1 false */
}

The else-if ladder

Syntax
if (condition1)
    block 1
else if (condition2)
    block 2
else if (condition3)
    block 3
else
    block n      /* none of the conditions was true */

The conditions are tested from top to bottom. The first true condition’s block runs, and the rest of the ladder is skipped. The final else is optional.

Because conditions are tested in order, each later test can assume the earlier ones were false. This keeps the conditions short.

The dangling else

When braces are omitted, which if does an else belong to? C’s rule: an else always pairs with the nearest preceding unmatched if in the same block. Indentation is ignored by the compiler.

C · dangling.c
#include <stdio.h>

int main(void)
{
    int a = 1, b = 1;

    if (a == 1)
        if (b == 0)
            printf("Hi\n");
    else
        printf("Bye\n");
    return 0;
}
Output
Bye

Despite the indentation, the else belongs to if (b == 0). Since a == 1 is true and b == 0 is false, the inner else runs and prints “Bye”. Use braces to make the intended pairing explicit.

Key takeaways

  • A nested if places one decision inside another.
  • An else if ladder tests conditions in order and stops at the first true one.
  • The final else handles every case that no condition matched.
  • An else belongs to the nearest unmatched if; use braces to be safe.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

In an else if ladder, how many branches can run?

Show answer

Answer: B. At most one

Testing stops at the first true condition, so at most one branch runs (exactly one if there is a final else).

Question 2 · multiple choice

With the corrected grade program, which grade does 75 receive?

Show answer

Answer: C. B

75 is not ≤ 50 and not < 60, but it is ≤ 75, so “Grade B” is printed. A needs marks above 75.

Question 3 · multiple choice

What does this program print?

C · nested_if.c
#include <stdio.h>

int main(void)
{
    int x = 10;
    if (x > 5) {
        if (x > 20)
            printf("A\n");
        else
            printf("B\n");
    } else {
        printf("C\n");
    }
    return 0;
}
Show answer

Answer: A. B

x > 5 is true, so the inner if runs; x > 20 is false, so its else prints B. C belongs to the outer else, which is skipped.

Question 4 · multiple choice

Without braces, which if does an else belong to?

Show answer

Answer: D. The nearest preceding unmatched if

C ignores indentation; the grammar pairs an else with the closest unmatched if.

Question 5 · multiple choice

What does this program print (Check Your Progress 1)?

C · assign_if.c
#include <stdio.h>

int main(void)
{
    int a, b = 0;
    if ((a = b = 1))
        printf("hello\n");
    else
        printf("world\n");
    return 0;
}
Show answer

Answer: D. hello

a = b = 1 assigns 1 to b and then to a, and the value of the whole assignment is 1, which is true. It is an assignment, not a comparison of a with b.

Question 6 · short answer

Write an else if ladder that prints the tax rate for an income: up to 3,00,000 → 0%; up to 7,00,000 → 5%; above that → 10%. Trace it for 5,00,000.

Show worked answer

if (income <= 300000) printf("0%%\n"); else if (income <= 700000) printf("5%%\n"); else printf("10%%\n"); (%% prints a percent sign). For 500000: the first test 500000 <= 300000 is false; the second test 500000 <= 700000 is true, so “5%” is printed and the rest of the ladder is skipped.

↑ ↓ move↵ openTab switch type