-2

exhausted noob here. I have basically 2 questions...

Sorry for clarification the switches are inside a loop which gets a bunch of numbers and then sorts them using OrderByDescending(). My problem is now I dont know which numbers there are, I only know that they are ordered by descending. Now I have a priority on numbers some specific number is more important for me than other (there is no rule or logic behind that). But on the other hand none of the numbers I am looking for could be inside, so as a "default" return or work with the highest number there was inside the collection.

1: are switch-cases order sensitiv? Assuming I switch on a range of 1 to 5 and for me 4 is of higher priority than the rest would that work as intended?

switch(number)
{
case 4: DoFancyStuff(); break;
case 3: NotTooFancy(); break;
case 1: StillOk(); break;
case 2: Bad(); break;
case 5: SuperBad(); break;
default:break;
}

Anyway, further lets assume number is ordered by descending. But I do not now know the possible range of number, it could be 2, 4, 8, 16, and so on but a number could be missing: 2, 4, 16, ...

So if I have to say

switch(number)
{
case 32: Nice(); break;
case 64: OkToo(); break;
case 128: Nah(); break;
case 8: OkStillTakeIt(); break;
default: break;
}

So as the numbers are ordered by descending order the big ones come first but they do not have the highest priority there could be a number coming with the highest (priority) one (first case) but it also does not have to... and if none fits I want to take the highest one but that is "lost".

How do I fix that? Or am I just overthinking? Wrong approach? My brain is exploding from thinking about it.

sceiler
  • 1,145
  • 2
  • 20
  • 35
  • 1
    Why don't you experiment with it first, then you should have your answer. – Der Kommissar Apr 21 '15 at 17:28
  • yeah do the downvotes get removed after I added my clarification which makes my question having more sense...? – sceiler Apr 21 '15 at 17:37
  • While I've written an answer to the top level question, you cover a lot of ground. I recommend going back and rethinking your approach and writing a new question with a clear definition of what you are trying to achieve and how your approach is failing you. – Steve Mitcham Apr 21 '15 at 18:01
  • I think duplicate covers a lot of information you are looking for in particular in [this](http://stackoverflow.com/a/48259/477420) answer. Note that in most practical case (several choices) you'll unlikely to see any differences and answers to this question cover that part pretty nicely. – Alexei Levenkov Apr 21 '15 at 18:04

4 Answers4

6

All cases in a switch must be mutually exclusive, so there's really no concept of priority. There's no way 2 cases could be eligible in the same switch.

For more information, see switch (C# Reference).

Jon Tirjan
  • 3,556
  • 2
  • 16
  • 24
  • yes, a single number is either 1 or 2, no number is both 1 and 2 at the same time. – DLeh Apr 21 '15 at 17:26
  • yes of course sorry. I forgot to add that the switches inside a for loop who loops through a collection of numbers among others – sceiler Apr 21 '15 at 17:28
0

Not answer: OP is not looking for range checks. Not deleting to stop others to answer the same.


It sounds like you expect switch to check ranges of values - it does not, it only allows to check specific values.

If you need to check ranges - several if checks is good option:

if (value < 10) {...} // 0-9
else if (value < 100) {...} //10-99
else {...} // 100+

For more ranges - sorted list of ranges and binary search may be better option

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • no i dont. sorry i added more info as clarification. I forgot to mention the loop and collection of numbers – sceiler Apr 21 '15 at 17:36
0

The order in a switch statement is totally irrelevant. Think of them like if statements, but they're all exclusive cases that cannot overlap. No number can be 1 and 2 at the same time, so if a 1 enters a switch statement, it will always go to the case 1, no matter where it is, and disregard any other cases.

For the sake of maintenance, I would recommend that you put them in order so your cases are easy to find.

DLeh
  • 23,806
  • 16
  • 84
  • 128
0

Priority is not a concept that matters in a switch statement. The short answer is that the code will work from top to bottom, checking your value against each of the cases in turn until one is found, or the default is hit.

Now that being said, order is often very important when you are talking about the frequency of a particular value. If you know from your design that a certain case is going to be much more frequent than the other numbers, then it makes sense for you to put that number as the first case in the switch statement to minimize the number of checks.

For certain scenarios, the compiler can optimize the switch statement into a constant time branch known as a jump table, in which case it wouldn't matter what order the cases were in.

Steve Mitcham
  • 5,268
  • 1
  • 28
  • 56
  • It's not necessarily true that a switch will compare the input value with each of the case values. Often (at least for integral types), the switch will translate into one or more (switch opcodes)[https://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.switch%28v=vs.110%29.aspx], which is a jump table. – Kyle Apr 21 '15 at 18:08
  • You link doesn't work, however, I was speaking conceptually, not necessarily about the underlying implementation – Steve Mitcham Apr 21 '15 at 18:39
  • [Here's a fixed link](https://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.switch%28v=vs.110%29.aspx) Also, your recommendation that order is important when talking about the frequency depends on the underlying implementation. A jump table can branch in a time roughly independently of the number of items in the table (within reason). That recommendation is not necessarily true. – Kyle Apr 21 '15 at 19:43
  • You have a point and I will adjust the answer for posterity. In general I wouldn't take that into account because whether the compiler chooses to implement a jump table would not make the execution worse, and in many cases the compiler wouldn't generate a jump table as an optimization. – Steve Mitcham Apr 21 '15 at 19:53