Forums Register Login

switch statement

+Pie Number of slices to send: Send


Why does this code produce "default" and not 1 ?
+Pie Number of slices to send: Send
That's because the unicode value of '1' will be taken to compare in the switch statement. This unicode value differs from 1, 2 and 3 so the default will be taken...

Greetz
+Pie Number of slices to send: Send
Geoffrey, I believe your explanation isn't quite correct.

The integer value of a char primitive is in fact the ASCII value of the character it represents. As it shows in the following code:



So even for char x = '1', the value x holds is the ASCII value of '1', which is 49.
[ May 25, 2004: Message edited by: Wendal Park ]
+Pie Number of slices to send: Send
Wendal, the char does not have to be a ASCII character like 'a' or '1', it could be some value like \u0546 (whatever that is) which is way out of the ASCII range.


[ May 25, 2004: Message edited by: Barry Gaunt ]
+Pie Number of slices to send: Send
Oh right, thanks for pointing out that.
I was just focusing on the single char (e.g. 'a') case which made me think of ASCII from the very beginning.
Police line, do not cross. Well, this tiny ad can go through:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 896 times.
Similar Threads
Switch statement
break using label
Default value in switch construct
AutoBoxing - Compilation Error
return in Switch case
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 00:45:09.