Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Practical 4: Menu Driven Program using Switch-Case

Lesson 5 of 35 in the free Web Based Programming Lab notes on Siksha Sarovar, written by Rohit Jangra.

Aim

To build a menu-driven calculator in PHP that dispatches the user's choice through a switch-case construct, including break, default and a division-by-zero guard.

Theory

A switch statement compares one subject expression against a series of case labels and starts executing at the first label that matches. Advanced points every examiner probes:

  • Loose comparison: switch matches cases with == semantics, so case "1" would match integer 1. PHP 8's match expression is the strict (===) alternative that also returns a value and needs no break.
  • Fallthrough: without break, execution continues into the next case's body. That is occasionally useful (grouping cases) but is a classic source of bugs — every case here ends with break.
  • default runs when nothing matches; placing it last is a convention, not a requirement.
  • Menu-driven design is the standard pattern for console/CGI utilities: display numbered options, read a choice, dispatch with switch, and loop until exit. It cleanly separates input, dispatch and action concerns.
  • Defensive programming: any arithmetic path that divides must guard the denominator ($b != 0) because integer division by zero throws a DivisionByZeroError in PHP 8.

Requirements

  • XAMPP/WAMP with PHP 8.x, or PHP CLI
  • Code editor (VS Code), browser or terminal

Procedure

  1. Start Apache from the XAMPP Control Panel.
  2. Save the snippet as p04_menu_switch.php in C:\xampp\htdocs\wbplab.
  3. Run http://localhost/wbplab/p04_menu_switch.php in the browser or php p04_menu_switch.php in a terminal.
  4. Change $b to 0 and observe the guarded division message; add a bogus choice such as 9 to $demoChoices to see default fire.

Explanation of the Code

  • $a = 20 and $b = 5 are the fixed operands; $demoChoices = [1, 2, 3, 4] simulates a user selecting every menu option in turn so the program is runnable without interactive input (in a real form, the choice would arrive via $_POST).
  • The menu itself is printed once with echo before the loop.
  • Inside foreach, switch ($choice) dispatches: case 1 prints the sum ($a + $b), case 2 the difference, case 3 the product.
  • case 4 demonstrates the guard: it divides only when $b != 0, otherwise prints Division by zero not allowed. — the check belongs inside the case so other operations remain unaffected.
  • Each case ends in break to prevent fallthrough, and default catches any invalid menu number.

Expected Output

After the four-line menu, the program prints four blocks: Choice: 1 / Addition: 25, Choice: 2 / Subtraction: 15, Choice: 3 / Multiplication: 100, Choice: 4 / Division: 4 — each block separated by a blank line.

🎯 Viva Questions

  1. What happens if break is omitted from a case? Execution falls through and runs the next case's statements too.
  2. How does switch compare values? Loosely (==), with type juggling; match in PHP 8 compares strictly.
  3. When does default execute? Only when no case label matches the subject expression.
  4. Why guard case 4 with $b != 0? Division by zero raises DivisionByZeroError (fatal) in PHP 8.
  5. switch vs match — two differences. match uses strict comparison and returns a value; it also has no fallthrough.
  6. Why is a menu-driven structure preferred in lab utilities? It separates input, dispatch and action, making each operation independently testable.

CO Mapping

CO1, CO2