• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

about switch case

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
one question i had about the switch case is that why it does not allowed float double and long in the switch case ?what's the actual reason behind this?
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The switch can hold a variable of an int compatibls primitive.
 
jinesh parikh
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Prasanna Vazza

thats what i am asking why they had made compulsory in switch case that switch expression must be of the type integer?
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Probably because comparing floating point values for equality is problematical. Floating point values are usually compared to be within a small range of each other, say 0.0000001. Exact equality is not really achievable at all possible values a floating point number can have.
 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.

Comparing floating-point numbers may lead to imprecision. In fact, this is probably a common rule in programming languages (well, at least in C++), because floating-point numbers are ruled by an IEEE (754) standard.
 
jinesh parikh
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear barry

can you give me any example of your statement please.......

because i am somewhat confused in your reply.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See #20 here for an introduction on floating point numbers, and a discussion of how they are imprecise. Specifically, with FP numbers the value "1.0" is not always equal to itself when it should be, e.g. 1.0 != 2.0/3.0 + 1.0/3.0.
 
jinesh parikh
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Ulf Dittmer,

Thank you very much for your reply

Now i understood why it was not allowed to use the floating points in the switch case expression.

my next question is this was for floating points but whats wrong with long data type as it was also not allowed in switch case expression.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic