Multiple If Statements

Hi,

I’m not really sure how to explain what I want so I hope this makes sense. If I had some code similar to this and need to create this multiple times (50+), how would I do it without writing it out every time. It would be multiple if statements with just two numbers different each time. The first if statement would have two values of 1, the second would have two values of 2, the third would have two values of 3 and so. I could write out all 50+ if statements but thought there could be an easier way other than that.

{
	if Health == (PlayerHealth - 1)
	{
		Something = 1;
	}
	if Health == (PlayerHealth - 2)
	{
		Something = 2;
	}
	if Health == (PlayerHealth - 3)
	{
		Something = 3;
	}
	if Health == (PlayerHealth - 4)
	{
		Something = 4;
	}
	if Health == (PlayerHealth - 5)
	{
		Something = 5;
	}
}

Thanks

This should work and is by far less stuff to write:

int iNumberOfIfStatements = 5;

for (int i = 0; i < iNumberOfIfStatements; ++i)
{
    if (Health == (PlayerHealth - i))
    {
        Something = i;
    }
}

First, how could Health == Health -5? that would never be true.

I think what you are looking for is a switch statment.

switch(Health){
    case(1):
    Something = 1;
   break;

  case(2):
  Something = 2;
  break;
}

Its like saying "check out Health… in case its 1 do this and break out of the switch statement, if not move on, in case its 2 do this and break out of the switch statement.

If the relation between the numbers is linear or maps an arithmetic function, you could do something like this:

something = health - playerhealth;

Else you can use a dictionary to map certain numbers to other numbers:

// Populate the dictionary with all relations you need
private Dictionary<int, int> somethingDictionary = new Dictionary<int, int>
{
    {0,0}, 
    {1,1}, 
    {2,4} 
};

// Returns the something that belongs to the given health and playerhealth
public int GetSomething(int health, int playerhealth)
{
    var index = health - playerhealth;
    return somethingDictionary[index];
}

For more informations about Dictionaries: See here

Hope one of this solutions can help you.

Greetings
Chillersanim