Possible Duplicate:
C/C++: switch for non-integers
Hi, I need to use a string in switch case. My solution so far was to calculate the hash of the string with my hash function. Problem is I have to manually pre-calculate all my hash values for strings. Is there a better approach?
h=_myhash (mystring); switch (h) { case 66452: ....... case 1342537: ........ }
If two cases in a 'switch' statement are identical, the second case will never be executed. This most likely indicates a copy-paste error where the first case was copied and then not properly adjusted.
No you can't.
The value of the expressions in a switch-case statement must be an ordinal type i.e. integer, char, short, long, etc. Float and double are not allowed. The case statements and the default statement can occur in any order in the switch statement.
Yes, we can use a switch statement with Strings in Java.
Just use a if() { } else if () { }
chain. Using a hash value is going to be a maintenance nightmare. switch
is intended to be a low-level statement which would not be appropriate for string comparisons.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With