Home > Enterprise >  (C#) How can I make all these "else if" statements more condensed and how can I optimize m
(C#) How can I make all these "else if" statements more condensed and how can I optimize m

Time:02-28

Okay, I have a load of else if statements, is there a way to condense them into fewer lines of code? Like make one if statement for all of it? And what ways could I make my code more optimize and easier to read?

            int x;
            int y;
            int time=0;
            Random rng = new Random();
            int hrand_num = rng.Next(-24000, 24000);
            int vrand_num = rng.Next(-24000, 24000);
            x = hrand_num;
            y = vrand_num;
            


            while (true)
            {
                ConsoleKey key = Console.ReadKey().Key;
                
                 
                if (key == ConsoleKey.UpArrow)
                {


                    y=y 2000;
                   
                }
                else if (key == ConsoleKey.DownArrow)

                {

                   y=y-2000;

                }
                else if (key == ConsoleKey.LeftArrow)
                {
                    x = x-1000;
                }
                else if (key == ConsoleKey.RightArrow)
                {
                    x = x 1000;
                }
                // Circumnavigate Players Position.
                  // North and South
                if (y >= 24001)
                {
                    y = -24000;
                }

                else if (y <= -24001)
                {
                    y = 24000;
                }
                  //West and East
                else if (x >= 24001)
                {
                    x = -24000;
                }
                else if (x <= -24001)
                {
                    x = 24000;
                }

                // Setting Time Zones

                if (x >= -2000 && x <= 0 )
                {
                    time = 0;
                }
                else if (x >=
                    1 && x <= 2000)
                {
                    time = 1;
                }
                else if (x >= 2001 && x <=4000)
                {
                    time = 2;
                }

                else if (x >= 4001 && x <= 6000)
                {
                    time = 3;
                }

                else if (x >= 6001 && x <= 8000)
                {
                    time = 4;
                }

                else if (x >= 8001 && x <= 10000)
                {
                    time = 5;
                }
                else if (x >= 10001 && x <= 12000)
                {
                    time = 6;
                }
                else if (x >= 12001 && x <= 14000)
                {
                    time =  7;
                }
                else if (x >= 14001 && x <= 16000)
                {
                    time =  8;
                }
                else if (x >= 16001 && x <= 18000)
                {
                    time =  9;
                }
                else if (x >= 18001 && x <= 20000)
                {
                    time =  10;
                }
                else if (x >= 20001 && x <= 22000)
                {
                    time =  11;
                }
                else if (x >= 22001 && x <= 24000)
                {
                    time = 12;
                }
                else if (x == -24000 && x <= -22001)
                {
                    time = 13;
                }
                else if (x >= -22000 && x <= -20001 )
                {
                    time = 14;
                }
                else if (x >= -20000 && x <= -18001)
                {
                    time = 15;
                }
                else if (x >= -18000 && x <= -16001)
                {
                    time = 16;
                }
                else if (x >= -16000 && x <= -14001)
                {
                    time =  17;
                }
                else if (x >= -14000 && x <= -12001)
                {
                    time = 18;
                }
                else if (x >= -12000 && x <= -10001)
                {
                    time = 19;
                }
                else if (x >= -10000 && x <= -8001)
                {
                    time =  20;
                }
                else if (x >= -8000 && x <= -6001)
                {
                    time = 21;
                }
                else if (x >= -6000 && x <= -4001)
                {
                    time = 22;
                }
                else if (x >= -4000 && x <= -2001)
                {
                    time = 23;
                }
 
                Console.WriteLine($"X: {x,6} Y: {y,6} Time: {time,3}");
            }   
         ```     

CodePudding user response:

Assuming that you are using C# 8.0 you may have a look at the switch statement and switch expressions: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/switch-expression (additionaly the patterns documentation is also helpful: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/patterns)

So you could write something like:

switch (key)
{
  case ConsoleKey.UpArrow:
    y=y 2000;
    break;
  // [...]
}

time = x switch
{
  >= -2000 and <= 0 => 0,
  >= 1 and <= 2000 => 1
  // [...]
};

CodePudding user response:

I would suggest that you take advantage of:

  • a switch statement for interpreting the arrow key
    • comparing the switch statement to an if loop, you can think of the first case as being the if condition and the remaning cases as being the else if conditions
  • Math.Abs( ) and Math.Sign( ) for circumnavigating the player's position
    • Math.Abs( ) returns the absolute value of the variable; e.g. will Math.Abs(x) return 5 both for x = 5 and x = -5
    • Math.Sign( ) returns the sign of the value; if the value is a negative number, it returns -1; if it's a positive number, it returns 1; if it's neither (0), it returns 0. This helps us determine the wanted sign of the updated value.
  • a switch expression for setting the time
    • seeing as the time value alone is determined by x in the end, you can use a switch expression rather than a switch statement to determine its value. The switch expression says that you want to determine the value of time based on the value of x; and each following condition is compared to x (<= -22001 is computed as x <= -22001). If the condition evaluates to true, the provided value is set as the value of time (=> 13 then sets time = 13).

It could be implemented like this:

int x;
int y;
Random rng = new Random();
int hrand_num = rng.Next(-24000, 24000);
int vrand_num = rng.Next(-24000, 24000);
x = hrand_num;
y = vrand_num;

while (true)
{
    switch (Console.ReadKey().Key)
    {
        case ConsoleKey.UpArrow:
            y  = 2000;
            break;
        case ConsoleKey.DownArrow:
            y -= 2000;
            break;
        case ConsoleKey.LeftArrow:
            x -= 1000;
            break;
        case ConsoleKey.RightArrow:
            x  = 1000;
            break;
    }

    // Circumnavigate Players Position.
    // North and South
    if (Math.Abs(y) > 24000)
    {
        y = -(Math.Sign(y) * 24000);
    }
    //West and East
    else if (Math.Abs(x) > 24000)
    {
        x = -(Math.Sign(x) * 24000);
    }

    // Setting Time Zones
    var time = x switch
    {
        <= -22001   => 13,
        <= -20001   => 14,
        <= -18001   => 15,
        <= -16001   => 16,
        <= -14001   => 17,
        <= -12001   => 18,
        <= -10001   => 19,
        <= -8001    => 20,
        <= -6001    => 21,
        <= -4001    => 22,
        <= -2001    => 23,
        <= 0        => 0,
        <= 2000     => 1,
        <= 4000     => 2,
        <= 6000     => 3,
        <= 8000     => 4,
        <= 10000    => 5,
        <= 12000    => 6,
        <= 14000    => 7,
        <= 16000    => 8,
        <= 18000    => 9,
        <= 20000    => 10,
        <= 22000    => 11,
        <= 24000    => 12,
        _ => 0
    };

    Console.WriteLine($"X: {x,6} Y: {y,6} Time: {time,3}");

I would also suggest introducing some constants; particularily for the value 24000.

  • Related