Solved

How to replace if - else statements with switch statements ?

Posted on 2015-02-16
2
107 Views
Last Modified: 2015-02-16
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()
        {
            Console.WriteLine("Please enter salary");
            string input = Console.ReadLine();

            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);
        }


    }
}

Open in new window

0
Comment
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
  • Learn & ask questions
2 Comments
 
LVL 14

Accepted Solution

by:
frankhelk earned 500 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

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

namespace EE_Q28617929
{
	class Program
	{
		static void Main(string[] args)
		{
			Console.WriteLine("Please enter salary");
			string input = Console.ReadLine();
			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);
		}
	}
}

Open in new window


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

-saige-
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JQuery Detecting page post back 6 30
Problem to Office 1 62
Using the .NET JSON Serializer to create a single string 6 51
Timeouts during development 3 25
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question