My teacher asked us to make a program in the most efficient way possible and to use a switch case for this.
The program asks the user for input, and depending on what the user inputs, the program will have to follow a set of instructions.
If the input is "A" or "a", the array has to be sorted from A to Z.
If the input is "Z" or "z", the array has to be sorted from Z to A.
If the input is "R" or "r", the array has to be reversed.
The array is a string[].
So I'm wondering if it's more effecient to use
switch (choice.ToLower())
{
case "a":
Array.Sort(array);
break;
case "z":
Array.Sort(array);
Array.Reverse(array);
break;
case "r":
Array.Reverse(array);
break;
}
or
if (choice.ToLower() == "a" || choice.ToLower() == "z")
{
Array.Sort(array);
}
if (choice.ToLower() == "r" || choice.ToLower() == "z")
{
Array.Reverse(array);
}
and also if this code can be optimised even further.
So, is the most efficient way to use a switch case, or the if structure as seen above and explain why?
I'm just curious since I always try to optimise all my code to full extent.