Convert Switch to IF

Asked

Viewed 163 times

3

Hello everyone is the following I have this code and I needed to change it to if instead of switch, how can I do this?

switch (theOperator)
{
     case "+":
         total2 = total1 + double.Parse(total3.ToString());
         break;
     case "-":
         total2 = total1 - double.Parse(total3.ToString());
         break;
     case "/":
         total2 = total1 / double.Parse(total3.ToString());
         break;
     case "*":
         total2 = total1 * double.Parse(total3.ToString());
         break;
     default:
         MessageBox.Show("Este simbolo não é reconhecido", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
         break;

2 answers

3


I will put the answer, but I make it clear that this IS NOT RECOMMENDED, the switch is much more readable and makes better use of memory than the statements if/else.

That said, the following block:

switch (theOperator) {
     case "+":
         total2 = total1 + double.Parse(total3.ToString());
         break;
     case "-":
         total2 = total1 - double.Parse(total3.ToString());
         break;
     case "/":
         total2 = total1 / double.Parse(total3.ToString());
         break;
     case "*":
         total2 = total1 * double.Parse(total3.ToString());
         break;
     default:
         MessageBox.Show("Este simbolo não é reconhecido", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
         break;
}

Turns into:

if (theOperator == "+") {
   total2 = total1 + double.Parse(total3.ToString());
}
else if (theOperator == "-") {
   total2 = total1 - double.Parse(total3.ToString());
}
else if (theOperator == "*") {
   total2 = total1 * double.Parse(total3.ToString());
}
else if (theOperator == "/") {
   total2 = total1 / double.Parse(total3.ToString());
}
else {
   MessageBox.Show("Este simbolo não é reconhecido", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
  • ok if the switch is better I’ll leave the switch but thanks anyway for the help

  • Why would it be more readable in this case?

  • At least for me, I think the switch is a much more readable way to understand the code, because Voce groups all the checks into simple conditionals

2

For anyone who wants a different option, how about?

if (theOperator == "+") {
           operacao = soma;
        }
        else if (theOperator == "-") {
           operacao = subtracao;
        }
        else if (theOperator == "*") {
           operacao = multiplicacao;
        }
        else if (theOperator == "/") {
           operacao = divisao;
        }
        else {
          MessageBox.Show("Este simbolo não é reconhecido", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        total2 = operacao(total1,double.Parse(total3.ToString()));  

Mine may be the least recommendable, but at least it’s more fun. Making use of encapsulated functions and lambda expressions. Here is the complete code:

using System;

public class Program
{
    public static void Main()
    {
        Calcula("+",1,2);
        Calcula("-",1,2);
        Calcula("*",1,2);
        Calcula("/",1,2);
    }

    public static void Calcula(string theOperator, double total1, double total3)
    {

        double total2 = 0;                          

        //Encapsulo os métodos
        Func<double,double,double> soma = (a,b) => a + b;
        Func<double,double,double> subtracao = (a,b) => a - b;
        Func<double,double,double> multiplicacao = (a,b) => a * b;
        Func<double,double,double> divisao = (a,b) => a / b;


        Func<double,double,double> operacao = null;


        if (theOperator == "+") {
           operacao = soma;
        }
        else if (theOperator == "-") {
           operacao = subtracao;
        }
        else if (theOperator == "*") {
           operacao = multiplicacao;
        }
        else if (theOperator == "/") {
           operacao = divisao;
        }
        else {
          MessageBox.Show("Este simbolo não é reconhecido", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        total2 = operacao(total1,double.Parse(total3.ToString()));  

        Console.WriteLine(total2);

    }   

}

I made an example compiling in https://dotnetfiddle.net/T01SsB

Browser other questions tagged

You are not signed in. Login or sign up in order to post.