IntelliJ Java IDE
The moose likes Java in General and the fly likes use of operators in switch Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "use of operators in switch" Watch "use of operators in switch" New topic
Author

use of operators in switch

Donald R. Cossitt
buckaroo
Ranch Hand

Joined: Jan 31, 2003
Posts: 401
Can anyone tell me what the logic behind JAVA not allowing the use of operators in the case method of switch is? EG

The alternative if | else if | else struct and their associated comparison operators are a pain in the ass to do and read later .
TIA
doco :roll:

(Marilyn added code tags)
[ February 02, 2003: Message edited by: Marilyn de Queiroz ]

doco
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1158

Does any language?
A switch statement is supposed to be a static comparison. It allows the compiler to do certain optimizations. Otherwise you might as well use an if-else chain.
Donald R. Cossitt
buckaroo
Ranch Hand

Joined: Jan 31, 2003
Posts: 401
Yes. Visual Basic, VBA, VBScript are ones I know of that allow that kind of behavior in Select Case myCase
Rich Raposa
Ranch Hand

Joined: Dec 06, 2001
Posts: 46
You're not serious, are you? Writing an "if/else if" is a pain in the ass compared to writing a switch statement?
Hmmm...next question, please.


Rich Raposa<br /><a href="http://www.javalicense.com" target="_blank" rel="nofollow">http://www.javalicense.com</a><br />Buy Java courseware -> get a free XBox!
 
 
subject: use of operators in switch
 
Threads others viewed
Recursive number problem
Help on a switch and others
switch: how to use one "case" for multiple/range option
If Statements
Variable might not have been initialized
IntelliJ Java IDE