Best use for handling multiple IF statements

I am wondering how best to do this: in my scenario, users choose from 1 to 5 options, depending on their options, which he should randomly choose between three.

Tell the user that they have Option 2 and 3 Selected, I could write an IF statement saying when they selected it, etc., the problem is I don't want to write IF statements for all possible combinations.

Just wondering which is the easiest way?

I could go to Switch, I think, but again, that would involve programming each individual combination.

Thank.

Edit: code for a small example

bool Option1 = false;
bool Option2 = false;
bool Option3 = false;

if (Option1 == true && Option2 == false && Option3 == false)
{
//Do Somthing
}
else if (Option1 == true && Option2 == true && Option3 == false)
{
//Do Somthing
}
else if (Option1 == true && Option2 == false && Option3 == true )
{
//Do Somthing
}

      

Etc..

+3


source to share


4 answers


It depends on what it is actually doing "// Do Somthing"

.

If there is some code duplication between parameters, then put it in a function and call it. Or split the duplicated code into a standalone block if ( Option1 ) { ... }

.



As you mentioned, you can include valid combinations in an enum (5 bits - 32 values) and put it in a switch statement.

+4


source


I would go for a nested if. you can always group them into functions if they get too unreadable:

if( option1 )
{
   if ( option2 )
   {
      if ( option3 )
      {
         doThing2();
      }
      else
      {
         doThing1(); // no more alternatives
      }
   }
   else
   {
      doThing1(); // need to do the same as above
   }
}
else
{
  ... more or less the same as above
}

      

I think that's the way it should be. And with good editors, it becomes easy to read ...



HTH

MNario

+4


source


Try to write your code so that it is closely related to the logic you are trying to model. Does the choice have to option1

change the meaning of other parameters or should they all be taken as a group?

You may find it helpful to introduce other flags (for example exactlyTwoOptionsSelected

) that you can then use in branching.

By the way, write if (option1 && option2)

or if (!option1 || option2)

etc. will be easier to work with than if (option1 == true && option2 == true)

and if(option1 == false || option2 == true)

.

+4


source


        Console.WriteLine("Enter first number: ");
        int first = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter second number: ");
        int second = int.Parse(Console.ReadLine());
        if (first % second == 0) 
        {
            Console.WriteLine("Number 1 is multiple of number 2.");
        }
        else if (second % first == 0)
        {
            Console.WriteLine("Number 2 is multiple of number 1.");
        }
        else
        {
            Console.WriteLine("Numbers are not multiples.");
        }
        Console.ReadKey();

      

0


source







All Articles