septillion:
Because it isn't anymore. The 'e' case is now inside a "conditional jump". But I guess (yes, I have to because no Arduino on hand ) it is valid to have cases inside an if() instead of more conventional other way around.
No, I am talking about the way the C++ language sees the body of a switch.
For C++ language the the body of a switch is just one continuous statement. You can place case labels anywhere inside that statement: inside inner if's, inside inner cycles. Anywhere.
There's nothing unusual about the original OP's code form the language's point of view.
The thing that's counterintuitive is that switch case looks highly structured such that each case has its own block of code but it really doesn't.
In reality, the switch uses the case labels as targets to jump to, so you can have all sorts of convoluted stuff in there with case labels in the middle of other constructs. Usually, you don't, because it would be a hideous thing to do and because every example you see makes you think that there is something neatly structured about a switch. Apart from fall through of course
wildbill:
The thing that's counterintuitive is that switch case looks highly structured such that each case has its own block of code but it really doesn't.
In reality, the switch uses the case labels as targets to jump to, so you can have all sorts of convoluted stuff in there with case labels in the middle of other constructs. Usually, you don't, because it would be a hideous thing to do and because every example you see makes you think that there is something neatly structured about a switch. Apart from fall through of course
Exactly. Moreover, we usually use {} statement as body of a switch. But we don't have to. It can be any statement. One can use a continuous if-statement as switch body like this
switch (code)
if (0) case 0: printf("Code zero\n");
else if (0) case 1: printf("Code one\n");
else if (0) case 2: printf("Code two\n");
And the functional beauty of this is that it naturally prevents fallthrough
UKHeliBob:
Have you tried declaring a variable inside a case ? It does not work unless you put { and } around the code for the case
No. That is a false and/or misleading statement.
Try this in C++
int code = 2;
switch (code)
{
case 0:
int a;
case 1:
double b;
case 2:
char c[20];
}
Report back to the forum.
In C you can declare variables "inside a case" without any restrictions. Just remember that in C declarations are not statements - you can't attach labels to them.
In C++ you can freely declare variables "inside a case" as long as these variables don't require initialization.
If you meet these requirements, you can freely declare variables anywhere inside switch body without any additional {}.
In C++ the governing rule is: don't jump into variable scope bypassing its initialization. This rule applies to all kind of jumps, not only to switch/case. As long as you meet this requirement, you are OK. Your assumption that in this case "{} around a case" is somehow "required" is incorrect. The {} is just one way to resolve the problem when it arises. It is definitely the most appropriate way to resolve it, but it is not a requirement.
If you don't read again inByte from the serial input it will never be 'z'.
So you have to read again with another Serial.read() before checking the 'z' condition.
Also, why are you storing the 'state' variable that you never use?