Search code examples
c#if-statementoptimizationlogicstatements

(C#) How can I make all these "else if" statements more condensed and how can I optimize my code better?


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}");
            }   
         ```     

Solution

  • Assuming that you are using C# 8.0 you may have a look at the switch statement and switch expressions: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/switch-expression (additionaly the patterns documentation is also helpful: https://learn.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
      // [...]
    };