[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
Solved

# How to replace if - else statements with switch statements ?

Posted on 2015-02-16
Medium Priority
113 Views
The following code calculates income tax.  Can it be replaced with "switch" and "case" statements ?

``````using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyFirstProject.day1.examples
{
class tax
{
static void Main()
{

double salary = Convert.ToDouble(input);
double tax;

if (salary <= 8350)
{
tax = 8350 * .10;
}
else if (salary <= 33950)
{
tax = (8350 * .10) + ((salary - 8350) * .15);
}
else
{
tax = (8350 * .10) + ((33950 - 8350) * .15) + ((salary - 33950) * .25);
}

Console.WriteLine("Tax = " + tax);
}

}
}
``````
0
Question by:naseeam
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 14

Accepted Solution

frankhelk earned 2000 total points
ID: 40612747
Not really. switch/case acts on fixed values, unsually integers, strings and enumertions (which are usually represented internally as integers.

The switch/case construct would therefore need the input variable to be converted ... i.e. with the if/then/elseif statement. That would be overcomplicated in your case ... but it would make sense if there are many segments in the code reacting on the same decision ... in that case it would make sense to compare once, stor the decision in an int (or an enumertion) and switch/case at multiple points in the code.
0

LVL 34

Expert Comment

ID: 40612932
To illustrate what frank is saying; consider the following:
``````using System;

namespace EE_Q28617929
{
class Program
{
static void Main(string[] args)
{
double salary = Convert.ToDouble(input);
double tax;
switch ((int)salary)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
// So forth and so on till you reached 8350
case 8350:
tax = 8350 * .10;
break;
case 8351:
case 8352:
case 8353:
case 8354:
case 8355:
case 8356:
case 8357:
case 8358:
case 8359:
case 8360:
// So forth and so on till you reached 33950
case 33950:
tax = (8350 * .10) + ((salary - 8350) * .15);
break;
default:
tax = (8350 * .10) + ((33950 - 8350) * .15) + ((salary - 33950) * .25);
break;
}
Console.WriteLine("Tax = " + tax);
}
}
}
``````

It is just far *too* inefficient to do this with a switch...case statement.

-saige-
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
###### Suggested Courses
Course of the Month12 days, 19 hours left to enroll