NBC 103 • Unit 212 min read★ High Exam Frequency
Conditionals (switch-case), Iteration Loops & Recursion
Unit 2: Control Statements, Loops & Modular Functions • Problem Solving Using C
👨🏫 Professor's Mental Model: The Road Intersection & Russian Nesting Dolls
if-else aur switch road ke signboards hain jo car ko sahi raste par bhejte hain. Loop ek round-about chowk hai jisme car tab tak ghoomti hai jab tak condition meet na ho. Aur Recursion Russian Matryoshka doll jaisa hai: ek badi doll ke andar waisi hi choti doll khulti hai jab tak sabse choti base doll na mil jaye!
Decision Making: if-else vs switch-case
switch-case evaluates an integer or char expression against constant case labels. Key rules:
1. Expression & case values MUST be integers or character constants (floats/strings illegal).
2. break statement prevents fall-through to subsequent cases.
3. default case executes when no match is found.
Loop Types: for, while & do-while
Entry-Controlled Loops (for, while): Condition checked BEFORE loop body (minimum iterations: 0).
Exit-Controlled Loop (do-while): Condition evaluated AFTER body (guarantees AT LEAST 1 execution).
Functions: Call by Value vs Recursion
Functions enable modularity and code reuse. In Call by Value, arguments are copied to formal parameters (modifications do not impact caller). Recursion occurs when a function calls itself directly or indirectly, requiring a base case to terminate.
Interactive Tested Code Example
#include <stdio.h>
// Recursive Factorial Function
long long factorial(int n) {
if (n <= 1) return 1; // Base case
return n * factorial(n - 1); // Recursive step
}
int main(void) {
int num = 6;
printf("Factorial of %d = %lld\n", num, factorial(num));
return 0;
}💡 Note:Calculates 6! = 720 recursively. Shows the critical role of the base case (n <= 1) in preventing infinite recursion and stack overflow.
🎯 University Exam Scoring Blueprint
- Always draw the recursive stack activation tree for factorial or Fibonacci.
- Explain that omitting break in a switch causes Fall-Through.
- Compare while vs do-while with syntax and flowchart.
Top Viva Questions on Conditionals (switch-case), Iteration Loops & Recursion
1