[Day 25] Breaking inside switch-win32_handmade.cpp

Sorry for printing out the code like this but I think it would make it easier to understand my question. Delete thread if necessary. Anyway, in win32_handmade.cpp, inside the Win32ProcessPendingMessages function, we have a switch statement for handling messages. inside it, there is this code:

1
2
3
4
5
            case WM_SYSKEYDOWN:
            case WM_SYSKEYUP:
            case WM_KEYDOWN:
            case WM_KEYUP:
                { ..


Without breaks between any of the cases, wouldn't WM_SYSKEYDOWN, WM_SYSKEYUP or WM_KEYDOWN trigger WM_KEYUP?

Edited by Class GenericHuman on
This isn't a day 25 thing. But anyway, yes, you're right. That's exactly what should happen.

Basically, we want to handle all of these messages with the same block of code.
Oh, okay. We are catching all those cases in one case. Thanks. Also, can you tell what the reason is for doing it this way? Is it style choice or does it affect performance/readability etc?
That's...the only way to do it that I know of?
The only alternative is to copy and paste the same block of code for each case, or put it in a separate function and call that in each case. Either way, this way is much easier.
If we don't include each of these cases, they will be caught by the default handler.
Alternative would be to use if statement:
1
2
3
4
if (Message==WM_SYSKEYDOWN || Message==WM_SYSKEYUP || Message==WM_KEYDOWN || Message==WM_KEYUP)
{
   ... // big block of code
}
EDIT: Overkill, explained much more succinctly above

Further reading:
http://msdn.microsoft.com/en-us/l...ws/desktop/ff468930(v=vs.85).aspx

Edited by Ruy Calderon on Reason: Overkill, explained much more succinctly above
Btw, you could look on switch statement as a series of comparisons and goto's, if that is easier to understand.

So code like this:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
switch (Var)
{
  case Value1:
    Code1;
    break;
  case Value2:
  case Value3:
  case Value4:
    Code2;
    break;
  default:
    Code3;
    break;
}


Is equivalent to this code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
if (Var == Value1)
{
   goto CaseA;
}
else if (Var == Value2 || Var == Value4 || Var == Value5)
{
   goto CaseB;
}
else
{
   goto CaseC;
}

CaseA:
  Code1;
  goto End;
CaseB:
  Code2;
  goto End;
CaseC:
  Code3;
  goto End;

End:
  ...


Of course writing switch and cases is much cleaner, and also in some cases compiler can optimize it better than series of if statements.

Edited by Mārtiņš Možeiko on
It's semantically equivalent, and that may help with understanding.

This is an aside, but even a Sufficiently Smart Compiler™ is unlikely to generate equivalent code. Many compiler writers are of the opinion that if any programmer writes that second thing, they deserve the generated code that they get.

The gotos will almost certainly be elided in a modern compiler based on SSA form, but some optimisation passes will have already run by the time it comes to disentangle the control flow.