Select Case DataInputNumber
Case 110 OR 300 OR 600 OR 1200 OR 2400 OR _
4800 OR 9600 OR 14400 OR 19200 OR 28800 OR _
38400 OR 56000 OR 57600 OR 115200
ComBaud = DataInputNumber
End Select
The error is
RunSetup.c warning: suggest parentheses around comparison in operand of |
The resulting compiled code does not function correctly, the case statement evaluates true on all values.
Is there a limit to how many possible values there can be for a Case statement? The reasoning behind the single case select statement is simply a matter of readability. See this thread for more information.
The ZBasic compiler did the correct thing according to the code you provided. A case statement can contain an expression and yours is one huge ORed expression. Instead the syntax you need is as follows:
Select Case DataInputNumber
Case Is = 110, Is = 300, Is= 600, Is = 1200, Is = 2400, _
Is = 4800, Is = 9600, Is = 14400, Is = 19200, Is = 28800, _
Is = 38400, Is = 56000, Is = 57600, Is = 115200
ComBaud = DataInputNumber
Case Else
ComBaud = DEFAULT_BAUD_DATE
End Select
I also added the selection for a default baud rate if that is needed. The resultant C code is as you would expect with a set of ORed conditions:
mikep wrote:The ZBasic compiler did the correct thing according to the code you provided.
While this may technically be true, it seems odd that the compiler would issue a warning about code itself generated. Now, if I wrote the C code, it would make sense that the warning should point out the improper usage of the case statement. In this case though, shouldn't the preprocessor give the warning (i.e. pointing the user to the ZBasic code rather than the C code)?
On the other hand, I think that the backend compile process has no way of determining the source of the C code it is trying to compile, in which case it cannot possibly know who wrote the faulty code.
In either case, thanks for the insight on the correct usage of the case statement. I'll check the rest of my code for similar errors.
Don_Kirby wrote:t seems odd that the compiler would issue a warning about code itself generated.
You've unearthed a situation where the generated code isn't quite what it should be. There should be an additional set of parentheses around the right hand operand of the == operator.
As to the original ZBasic code, this is a situation where the code you wrote is syntactically correct but doesn't accomplish what you intended.
The variation of Mike's suggestion shown below performs identically; the Is = is superfluous.