Usually, when using the switch you cannot define and initialize local variables for a compound statement, for example
switch (a) { int b = 5; case 1: break; default: break; }
However, since the switch is an expression of type for or while , there is no rule against using the compound statement here for examples . But that would mean that the label could be used between the closing bracket after the switch keyword and the opening bracket.
So, in my opinion, it would be possible and allowed to use the switch as follows:
switch (a) default: { int b = 5; break; case 1:
My question is: is initialization necessary in this case (a! = 1)? From what I know about standards, yes, it should be, but I canβt find it directly in any of the documents available to me. Can anyone give a definitive answer?
And before I get comments on this question, yes, I know that this is not a way of programming in the real world. But, as always, I'm interested in the boundaries of the language specification. I would never tolerate such a style in my development team!
source share