Active Logic: Switch-Case Statements
Master menu-driven programming, break statement mechanics, fallthrough behavior, default fallback handling, and multi-tier billing calculations.
Switch-Case Syntax Rules
| Component | Syntax Rule | Key Trap / Pitfall |
|---|---|---|
| Expression Type | switch (expr) | Must evaluate to int or char! (No float/double) |
| Case Labels | case constant: | Must be compile-time constants (e.g. case 1: or case 'A':) |
| Break Statement | break; | Omitting break causes fallthrough into next cases! |
| Default Case | default: | Executes when no case constant matches |
๐ฏ Learning Philosophy: Active Logic Building
Test your menu-driven logic across 3 Interactive Learning Modes:
- ๐ง Tab 1 (Logic & Hints): Concept breakdown & fallthrough rules.
- ๐งฉ Tab 2 (Logic Puzzle): Complete missing
switch,case,break, &defaultkeywords! - ๐ป Tab 3 (C Solution): Reference verified C code solutions.
Menu-Driven Area Calculator (Circle, Rectangle, Triangle)
Select shapes from a menu (1=Circle, 2=Rectangle, 3=Triangle) and branch to the correct formula using switch-case.
Shape Formulas
- Case 1 (Circle):
area = 3.14159 * r * r - Case 2 (Rectangle):
area = l * w - Case 3 (Triangle):
area = 0.5 * b * h - Default: Invalid choice notification
Interactive Logic Hints
break;!๐งฉ Complete the Switch-Case Menu Code
1:
area = 3.14159 * r * r;
;
:
printf("Invalid");
}
๐ Area Menu Calculator
Fallthrough Experiment (Switch Without Break)
Demonstrates what happens when break statements are omitted: execution "falls through" to all subsequent cases!
Fallthrough Behavior
- When a matching
caseis found, C jumps to it. - Without
break;, CPU does NOT re-check case labels — it simply executes every line below it until the end of switch!
Interactive Logic Hints
case 'a': case 'e': case 'i': ...).๐งฉ Fallthrough & Break Practice Puzzle
Fill in C keywords below to control fallthrough execution in switch statement:
case 1: printf("Circle Area\n");
case 2: printf("Rectangle Area\n");
printf("Invalid Choice\n");
}
๐งช Test Fallthrough Interactively
Day of Week & Arithmetic Calculator
Translate day numbers (1=Sunday..7=Saturday) and perform menu-driven arithmetic operations (+, -, *, /).
Char Switch Case
- Operands for calculator:
switch (op)whereopis char case '+': res = a + b; break;case '/': res = a / b; break;(Check b != 0)
Interactive Logic Hints
๐งฉ Complete the Arithmetic Operator Switch
switch (op) {
case :
res = a + b; ;
case :
res = a / b; break;
}
๐ Day Translator & Calculator
Internet Browsing Bill Calculator
Computes browsing charges: 1 hour = Rs 20, 1 minute = Rs 1, Unlimited Day Pass = Rs 200 using switch-case menu.
Pricing Rates
- Option 1 (Hours): Bill = Hours * Rs 20
- Option 2 (Minutes): Bill = Minutes * Rs 1
- Option 3 (Day Pass): Flat Rs 200
Interactive Logic Hints
๐งฉ Complete the Internet Bill Switch Logic
case 1: bill = hours * ; break;
case 2: bill = mins * ; break;
case 3: bill = ; break;
}
๐ Internet Bill Calculator
Tiered Electricity Bill Calculator (Switch Category)
Computes electricity charges based on unit slabs: Tier 1 (1โ100 units = Rs 3/unit), Tier 2 (101โ200 units = Rs 5/unit), Tier 3 (>200 units = Rs 8/unit).
Tier Slabs
- Category 1 (1-100 units):
bill = units * 3.0 - Category 2 (101-200 units):
bill = 300 + (units - 100) * 5.0 - Category 3 (>200 units):
bill = 800 + (units - 200) * 8.0
Interactive Logic Hints
category = (units <= 100) ? 1 : ((units <= 200) ? 2 : 3);!๐งฉ Complete Tiered Electricity Calculation
case 1: bill = units * ; break;
case 2: bill = 300 + (units - 100) * ; break;
case 3: bill = 800 + (units - 200) * ; break;
}
โก Electricity Bill Calculator
Shopping Discount Calculator (Customer Tier)
Applies customer type discounts: VIP (20% discount), Regular (10% discount), Student (15% discount) using switch-case.
Discount Rules
- 'V' (VIP): 20% off →
finalAmount = amount * 0.80 - 'S' (Student): 15% off →
finalAmount = amount * 0.85 - 'R' (Regular): 10% off →
finalAmount = amount * 0.90
Interactive Logic Hints
(1.0 - discountRate)!๐งฉ Complete the Discount Switch Logic
case 'V': disc = ; break;
case 'S': disc = ; break;
case 'R': disc = ; break;
}