🧪
Lab 4 — Active Switch-Case & Menu Logic
Lab 4: Intro 🏠 Home
PSUC Lab 4 — Menu-Driven Control Structures

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

ComponentSyntax RuleKey Trap / Pitfall
Expression Typeswitch (expr)Must evaluate to int or char! (No float/double)
Case Labelscase constant:Must be compile-time constants (e.g. case 1: or case 'A':)
Break Statementbreak;Omitting break causes fallthrough into next cases!
Default Casedefault:Executes when no case constant matches

๐ŸŽฏ Learning Philosophy: Active Logic Building

Test your menu-driven logic across 3 Interactive Learning Modes:

  1. ๐Ÿง  Tab 1 (Logic & Hints): Concept breakdown & fallthrough rules.
  2. ๐Ÿงฉ Tab 2 (Logic Puzzle): Complete missing switch, case, break, & default keywords!
  3. ๐Ÿ’ป Tab 3 (C Solution): Reference verified C code solutions.
Solve the switch-case logic puzzles before viewing reference code!
Exercise 1 of 6

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

๐Ÿ’ก Hint 1: What happens if break is omitted after case 1? โ–ผ
C will execute case 1 AND fallthrough directly into case 2 and case 3! Always include break;!

๐Ÿงฉ Complete the Switch-Case Menu Code

(choice) {
   1:
    area = 3.14159 * r * r;
    ;
  :
    printf("Invalid");
}
Select keywords above and click Check!

๐Ÿ“ Area Menu Calculator

Result will appear here...
ex1_area.cReference C Code
#include <stdio.h> #define PI 3.14159 int main() { int choice; float r, l, w, b, h, area; printf("1. Circle\n2. Rectangle\n3. Triangle\nEnter choice: "); scanf("%d", &choice); switch (choice) { case 1: printf("Enter radius: "); scanf("%f", &r); area = PI * r * r; printf("Area of Circle = %.2f\n", area); break; case 2: printf("Enter length & width: "); scanf("%f %f", &l, &w); area = l * w; printf("Area of Rectangle = %.2f\n", area); break; case 3: printf("Enter base & height: "); scanf("%f %f", &b, &h); area = 0.5 * b * h; printf("Area of Triangle = %.2f\n", area); break; default: printf("Invalid Choice!\n"); } return 0; }
Exercise 2 of 6

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 case is 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

๐Ÿ’ก Hint 1: Is fallthrough always a bug? โ–ผ
No! Intentional fallthrough is used to group multiple cases together (e.g. case 'a': case 'e': case 'i': ...).

๐Ÿงฉ Fallthrough & Break Practice Puzzle

Fill in C keywords below to control fallthrough execution in switch statement:

switch (choice) {
  case 1: printf("Circle Area\n");
  case 2: printf("Rectangle Area\n");
   printf("Invalid Choice\n");
}
Select statements above and click Check!

๐Ÿงช Test Fallthrough Interactively

Execution sequence...
ex2_fallthrough.cReference C Code
#include <stdio.h> int main() { int choice = 1; switch (choice) { case 1: printf("Circle Area\n"); // No break! case 2: printf("Rectangle Area\n"); // No break! case 3: printf("Triangle Area\n"); // No break! default: printf("Invalid Choice\n"); } return 0; }
Exercise 3 of 6

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) where op is char
  • case '+': res = a + b; break;
  • case '/': res = a / b; break; (Check b != 0)

Interactive Logic Hints

๐Ÿ’ก Hint 1: Can we use char in switch statements? โ–ผ
Yes! In C, characters are internally stored as integer ASCII values (e.g. '+' is ASCII 43).

๐Ÿงฉ Complete the Arithmetic Operator Switch

char op = '+'; float a = 10, b = 5;
switch (op) {
  case :
    res = a + b; ;
  case :
    res = a / b; break;
}
Select options above and click Check!

๐Ÿ“… Day Translator & Calculator

Result will show here...
calc_switch.cReference C Code
#include <stdio.h> int main() { char op; float a = 10, b = 5; scanf(" %c", &op); switch (op) { case '+': printf("Result = %.2f\n", a + b); break; case '-': printf("Result = %.2f\n", a - b); break; case '*': printf("Result = %.2f\n", a * b); break; case '/': if (b!=0) printf("Result = %.2f\n", a / b); break; default: printf("Invalid Operator\n"); } return 0; }
Exercise 4 of 6

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

๐Ÿ’ก Hint 1: What menu choice structure works best? โ–ผ
Prompt user for Choice (1-3), then input duration inside the specific case!

๐Ÿงฉ Complete the Internet Bill Switch Logic

switch (choice) {
  case 1: bill = hours * ; break;
  case 2: bill = mins * ; break;
  case 3: bill = ; break;
}
Verify rate values above and click Check!

๐ŸŒ Internet Bill Calculator

Bill output...
internet_bill.cReference C Code
#include <stdio.h> int main() { int choice, units; float bill = 0; printf("1. Hours 2. Mins 3. Unlimited\nChoice: "); scanf("%d", &choice); switch (choice) { case 1: scanf("%d", &units); bill = units * 20.0; break; case 2: scanf("%d", &units); bill = units * 1.0; break; case 3: bill = 200.0; break; default: printf("Invalid Plan\n"); } printf("Total Bill = Rs. %.2f\n", bill); return 0; }
Exercise 5 of 6

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

๐Ÿ’ก Hint 1: How to convert continuous units into switch cases? โ–ผ
Use integer category mapping: category = (units <= 100) ? 1 : ((units <= 200) ? 2 : 3);!

๐Ÿงฉ Complete Tiered Electricity Calculation

switch (category) {
  case 1: bill = units * ; break;
  case 2: bill = 300 + (units - 100) * ; break;
  case 3: bill = 800 + (units - 200) * ; break;
}
Verify tier rates above and click Check!

โšก Electricity Bill Calculator

Bill breakdown...
electricity_bill.cReference C Code
#include <stdio.h> int main() { int units, category; float bill; printf("Enter units consumed: "); scanf("%d", &units); category = (units <= 100) ? 1 : ((units <= 200) ? 2 : 3); switch (category) { case 1: bill = units * 3.0; break; case 2: bill = 300.0 + (units - 100) * 5.0; break; case 3: bill = 800.0 + (units - 200) * 8.0; break; } printf("Electricity Bill = Rs. %.2f\n", bill); return 0; }
Exercise 6 of 6

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

๐Ÿ’ก Hint 1: How to compute net payable amount? โ–ผ
Multiply total amount by (1.0 - discountRate)!

๐Ÿงฉ Complete the Discount Switch Logic

switch (custType) {
  case 'V': disc = ; break;
  case 'S': disc = ; break;
  case 'R': disc = ; break;
}
Verify rates above and click Check!

๐Ÿ›๏ธ Shopping Discount Calculator

Discount output...
shopping_discount.cReference C Code
#include <stdio.h> int main() { char type; float amount, disc = 0.0, finalAmt; printf("Enter type (V/S/R) & amount: "); scanf(" %c %f", &type, &amount); switch (type) { case 'V': case 'v': disc = 0.20; break; case 'S': case 's': disc = 0.15; break; case 'R': case 'r': disc = 0.10; break; default: printf("Invalid Type\n"); } finalAmt = amount * (1.0 - disc); printf("Discount = %.2f, Net Payable = Rs. %.2f\n", amount * disc, finalAmt); return 0; }
Knowledge Check

Test Your Switch-Case Control Mastery

Question 1 of 5
Loading question...