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
-
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); }
- 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; }
- Since the
default
section is needed to throw the exception from, it cannot be afinal switch
statement anymore. Here are the parts of the program that are modified:// ... switch (op) { // ... default: throw new Exception("Invalid operation: " ~ op); } // ...
Copyright © 1999-2025 by the D Language Foundation | Page generated by
Ddoc on Sun Aug 24 07:18:00 2025