Convert Switch to IF

3

Hello everyone is this 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;
    
asked by anonymous 03.08.2016 / 11:50

2 answers

3

I'll put the answer, but I make it clear that this is NOT RECOMMENDED , switch is much more readable and makes better use of memory than 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);
}
    
03.08.2016 / 12:25
2

For those who want a different option, what 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 did an example compiling on link

    
03.08.2016 / 15:35