NBC 103 • Unit 215 min readHigh Exam Frequency

Decision Making Statements: if-else Ladder & switch-case

Unit 2: Control Statements, Loops & Modular FunctionsProblem Solving Using C

👨‍🏫 Professor's Mental Model: The Railway Track Switcher & Multi-Floor Elevator Console

if-else statement railway track badalne jaisa hai: agar signal green hai toh train main line par jayegi, warna loop line par (True/False). Aur switch-case ek high-speed elevator ke keypad jaisa hai: aapne seedha '5' dabaya aur elevator direct 5th floor par pahunch gayi, use 1st, 2nd, 3rd, 4th floor check karne ki zaroorat nahi padti (Direct Jump Table)! Lekin agar har case ke baad 'break' nahi lagaya, toh lift har floor par rukte hue niche tak slip ho jayegi (Fall-Through)!

Academic Lecture Notes & Solved Study Pages

Unit 2 • Core Concepts, Step-by-Step Proofs & Notebook Solutions

3 Notebook Pages
NOTEBOOK PAGE 1 OF 3

1. The 4 Forms of Conditional Branching (if Constructs)

Simple if: Executes a statement block only when the condition evaluates to true (non-zero).
if-else: Provides a binary fork—executes Block A if condition is true, Block B if false.
Nested if-else: Placing an if statement inside another if or else block for multi-tiered filtering.
else-if Ladder: Multi-path decision structure evaluated sequentially from top to bottom until the first true condition is met.
The Dangling Else Problem:

Occurs when an else clause is ambiguously placed in nested if statements without curly braces. In C, the compiler matches the dangling else with the CLOSEST UNPAIRED preceding if statement. Always use explicit curly braces `{}` to eliminate ambiguity!

NOTEBOOK PAGE 2 OF 3

2. The switch-case Statement: Architecture & Rules

Syntax: switch (expression) { case constant_1: statements; break; case constant_2: statements; break; default: default_statements; }

Strict Rules of switch-case:

Expression & Case Labels: The switch expression and case values MUST evaluate strictly to integer or character types (`int` or `char`). Floating-point numbers (`float`, `double`) and strings are ILLEGAL.
Case Labels must be constant literals or compile-time constant expressions (e.g. `case 5:`, `case 'A':`, `case 2 + 3:`). Variables are NOT allowed (e.g., `case x:` is a compiler error).
Unique Cases: Two case labels cannot have duplicate values within the same switch.
Default Clause: Optional, executes when no case matches. It can appear anywhere, though convention places it at the end.
NOTEBOOK PAGE 3 OF 3

3. The Critical Role of break & Fall-Through Phenomenon

In C, case labels act merely as entry markers into the switch body.

If a matching case is found, execution enters and proceeds downward continuously across all subsequent cases until it encounters a `break;` statement or reaches the end of the switch block.
Fall-Through: Omitting `break` causes execution to fall through and execute unintended subsequent cases.
Intentional Fall-Through: Used productively when multiple distinct cases share the identical execution logic (e.g., grouping vowel checks `case 'a': case 'e': case 'i': case 'o': case 'u':`).
Comparative Matrix: if-else Ladder vs switch-case Statement
Parameterelse-if Ladderswitch-case Statement
Data Types AllowedAny data type (integers, floats, relational expressions, pointers)Only integer and character constants (no float/double/string)
Expression TypesRelational (<, >, <=) and Logical (&&, ||) ranges allowedOnly strict equality comparisons (==) against fixed constants
Execution SpeedSlower: Evaluates every condition sequentially O(N)Faster: Compiler constructs an indexed Jump Table O(1)
Readability & MaintenanceBecomes cluttered and hard to read for >5 menu optionsExtremely clean, structured, and ideal for menu-driven programs
Default HandlingFinal optional else block catches unmatched casesOptional default: label executes on no match

Interactive Tested Code Example

#include <stdio.h>

int main(void) {
    int option = 2;
    double n1 = 20.0, n2 = 4.0, result;

    printf("=== MENU-DRIVEN ARITHMETIC ENGINE ===\n");
    printf("1. Add  |  2. Subtract  |  3. Multiply  |  4. Divide\n");
    printf("Selected Option: %d\n", option);

    switch (option) {
        case 1:
            result = n1 + n2;
            printf("Result: %.2f + %.2f = %.2f\n", n1, n2, result);
            break;
        case 2:
            result = n1 - n2;
            printf("Result: %.2f - %.2f = %.2f\n", n1, n2, result);
            break;
        case 3:
            result = n1 * n2;
            printf("Result: %.2f * %.2f = %.2f\n", n1, n2, result);
            break;
        case 4:
            if (n2 == 0.0) {
                printf("Error: Division by zero is mathematically undefined!\n");
            } else {
                result = n1 / n2;
                printf("Result: %.2f / %.2f = %.2f\n", n1, n2, result);
            }
            break;
        default:
            printf("Error: Invalid option selected. Please choose 1-4.\n");
    }
    return 0;
}
💡 Note:Implements a menu-driven calculator using switch-case. Demonstrates case matching, mandatory break usage to avoid fall-through, and defensive zero-check.

🎯 University Exam Scoring Blueprint

  • In university exams, always highlight that float/double values are strictly prohibited in switch expressions.
  • Draw flowcharts comparing the sequential diamond test of else-if vs the single multi-way jump of switch.
  • Demonstrate intentional fall-through with an example (e.g. checking whether an input character is a vowel).

Top Viva Questions on Decision Making Statements: if-else Ladder & switch-case

2 Questions
1

Can we use relational conditions like 'case > 5:' in a C switch statement?

2

What is the Dangling Else problem and how does the C compiler resolve it?