• Post Reply Bookmark Topic Watch Topic
  • New Topic

Nested If, ElseIf vs. Switch and conditional ? question  RSS feed

 
Nathan Leniz
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have some code that uses quite a bit of if, else if statements, and I'm wondering if it would be more appropriate to use a switch in certain situations. Example:



and then I have this



Would it be more appropriate to use a switch statement on the second? From what I'm reading, the first block couldn't be used in a switch statement? Also, is it bad practice to use the =()?: kind of statement?


And if it is acceptable to use that kind of statement, can I pass it a more beefy argument like


Sorry for the flurry of questions all at once.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This sounds an awful lot like a slight extension of this thread. I think we've already addressed many of your questions there. Perhaps you could post your remaining, related questing there, as a followup?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nathan Leniz:
...From what I'm reading, the first block couldn't be used in a switch statement? ...

You could, although cases must be int compatible (not boolean), so ranges are cumbersome to handle. Specifically, you can "stack" cases for an "or" effect by not including "break" statements, allowing execution to fall through.

I'll leave your ternary question to the thread Jim mentioned, since I see it's posted there.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!