Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Range in switch?

Status
Not open for further replies.

tonedef

Programmer
May 24, 1999
64
US
Can I use a range in a case of a switch statement? ex.<br><br>&nbsp;&nbsp;&nbsp;<FONT FACE=monospace><b>switch</b> (integervalue)<br>&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>case</b> <i>1..10</i>:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>break</b>;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>case</b> <i>11..20</i>:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>break</b>;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>default</b>:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//<br>&nbsp;&nbsp;&nbsp;&nbsp;}</font>
 
Nope, sorry, but that's a good question. I know that's allowed in some languages, but not Java- java gives you an &quot;Invalid character in number&quot; error, seeing as how 1..10 is not a valid number. (It's just the second dot that's invalid- if you had 1.0, you'd get an &quot;Incompatible type for case&quot; error seeing as how 1.0 is a double and you've got an int.) However, what you can do (and I know this doesn't look pretty, but it's legal) is this:<br><br>switch (integervalue)<br>&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case 11: case 12: case 13: case 14: case 15: case 16: case 17: case 18: case 19: case 20:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;default:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>this is why you don't always use switch statements- best to do this:<br><br>if ((integervalue &gt; 0) && (integervalue &lt;= 10)) {<br>&nbsp;&nbsp;&nbsp;&nbsp;//<br>} else if ((integervalue &gt; 10) && (integervalue &lt;= 20)) {<br>&nbsp;&nbsp;&nbsp;&nbsp;//<br>}<br><br>Switch blocks are nice, but they're really only meant to make code look cleaner. If they don't accomplish that goal, then you should go with the if block. Unfortunately, the nice-looking switch block you'd like to go with is not supported by Java, so it's best to go with the if block.<br><br>Best of luck! <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence."
 
imotic,<br><br>Thanks.&nbsp;&nbsp;That's pretty much what I figured.&nbsp;&nbsp;I just had alot of choices for nested if..elses and the ranges were big enough I wanted to avoid the multiple case logic.<br><br>Oh well
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top