switch and case Solutions - D Programming Language
Description: D Programming Language
Keywords: D programming language

Menu

- Learn
- Documentation
  - Language Reference
  - Library Reference
  - Command-line Reference
  - Feature Overview
  - Articles
- Downloads
- Packages
- Community
  - Blog
  - Orgs using D
  - Twitter
  - Calendar
  - Discord (community-run)
  - Forums
  - IRC
  - Wiki
  - Issues
  - Contributing
  - Contributors
  - Foundation
  - Security Team
  - Donate
  - Sponsors
- Resources
  - Tour
  - Books
  - Tutorials
  - Tools
  - Editors
  - IDEs
  - run.dlang.io
  - Visual D
  - Acknowledgments
  - D Style
  - Glossary
  - Sitemap

Search
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

# switch and case Solutions

#### `switch` and `case` Dersi Problem Çözümleri

1.     import std.stdio;
       import std.string;

       void main() {
           string op;
           double first;
           double second;

           write("Please enter the operation: ");
           op = strip(readln());

           write("Please enter two values separated by a space: ");
           readf(" %s %s", &first, &second);

           double result;

           final switch (op) {

           case "add":
               result = first + second;
               break;

           case "subtract":
               result = first - second;
               break;

           case "multiply":
               result = first * second;
               break;

           case "divide":
               result = first / second;
               break;
           }

           writeln(result);
       }

2. By taking advantage of distinct `case` values:

           final switch (op) {

           case "add", "+":
               result = first + second;
               break;

           case "subtract", "-":
               result = first - second;
               break;

           case "multiply", "*":
               result = first * second;
               break;

           case "divide", "/":
               result = first / second;
               break;
           }

3. Since the `default` section is needed to throw the exception from, it cannot be a `final switch` statement anymore. Here are the parts of the program that are modified:

       // ...

           switch (op) {

           // ...

           default:
               throw new Exception("Invalid operation: " ~ op);
           }

       // ...

Copyright © 1999-2026 by the D Language Foundation | Page generated by Ddoc on Wed Aug 5 11:24:17 2026