File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes use of operators in switch Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Barcodes with iOS this week in the iOS forum
or Core Java for the Impatient in the Java 8 forum!

JavaRanch » Java Forums » Java » Java in General
Bookmark "use of operators in switch" Watch "use of operators in switch" New topic

use of operators in switch

Donald R. Cossitt
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 .
doco :roll:

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

Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

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
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? question, please.

Rich Raposa<br /><a href="" target="_blank" rel="nofollow"></a><br />Buy Java courseware -> get a free XBox!
I agree. Here's the link:
subject: use of operators in switch