• Post Reply Bookmark Topic Watch Topic
  • New Topic

When to use switch selection statement?  RSS feed

 
John Stuart
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any situation where using switch is a must to solve a problem?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Stuart wrote:Is there any situation where using switch is a must to solve a problem?


I don't know about a "must" but when I have more than 2 possible values to direct what gets executed next I tend to use switch.

I find that a well documented switch statement is easier to understand than a whole bunch of if( x == someconstant )statements

I tend to use "state machine" architecture a lot and switch is ideal.

Bill
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a limited gamut of options which can all be specified exactly in advance (compile‑time constants), then you can consider using a switch. Have you read about it in the Java™ Tutorials?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!