• Post Reply Bookmark Topic Watch Topic
  • New Topic

switch statement

 
Angela lewis
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why does this code produce "default" and not 1 ?
 
Geoffrey Vlassaks
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Wendal Park
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Wendal Park
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!