• 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

Puzzling over switch behavior

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Silly question, but I need to ask it.

Given the following code:

The output of this is:
120
121
127

Of course what happens is that control hits the test for 120 and prints 120. Because there is no "break" associated with that case, then flow continues with the 121 and 127 tests, exiting with the break in the 127 case.
But it is counter-intuitive that the println's associated with 121 and 127 would actually execute. It seems more logical that the makers of Java would have decided that statements associated with non-matches would not execute. So in other words, in this example, 120 does not match 121 so it is more logical that the println("121") should not execute.

So I'm wondering why the makers of Java would allow a switch statement to behave this way. Does anyone have any thoughts?

I hope that made sense. (Ravings of a mad man )

Kind regards,
Steve




 
Author and all-around good cowpoke
Posts: 13078
6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually I have found that "fall through" operation to be quite convenient from time to time so I like the requirement for break. Sometimes "return" is quite convenient also.

Bill
 
Ranch Hand
Posts: 258
2
IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It may be useful for multiple matches
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephen-Austin Murphy wrote:But it is counter-intuitive that the println's associated with 121 and 127 would actually execute. It seems more logical that the makers of Java would have decided that statements associated with non-matches would not execute...So I'm wondering why the makers of Java would allow a switch statement to behave this way. Does anyone have any thoughts?


It goes back to the origins of Java, which was based on C/C++, and the syntax of the switch statement in Java is almost identical to that of C (except that Duff's device is not allowed).

I agree that it's not the best, and to this day I still sometimes forget to put in the break; but as William says, there are times when the "fallthrough" is quite useful.

Winston
 
Stephen-Austin Murphy
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Winston and Raymond. It makes more sense now.
 
I'm just a poor boy, I need no sympathy, because I'm easy come, easy go, little high, little low, little ad
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic