My teacher asked us to make the program the most efficient way and use the switching case for this.
The program asks the user for data entry, and depending on what the user enters, the program will have to follow a set of instructions.
If the input is "A" or "a", the array must be sorted from A to Z.
If the input signal is "Z" or "z", the array must be sorted from Z to A.
If the input is "R" or "r", the array must be canceled.
An array is a string [].
So, I am wondering if it’s more efficient 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 optimized even further.
So, the most efficient way to use the case of a switch or if structure, as shown above, and explain why?
I'm just curious because I always try to fully optimize my entire code.
source share