File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes complex conditions in switch statement is possible? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "complex conditions in switch statement is possible?" Watch "complex conditions in switch statement is possible?" New topic
Author

complex conditions in switch statement is possible?

Mahendran Aiyappan
Ranch Hand

Joined: Sep 05, 2005
Posts: 102
Hi Guys,

Is it possible to do the following condition in switch statement?
I know its crazy to do this using switch but my question is what level of support does switch statement provides.



Thanks,
Mahendran
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42281
    
  64
See "SwitchLabel" in the JLS: http://java.sun.com/docs/books/jls/third_edition/html/statements.html#258896


Ping & DNS - my free Android networking tools app
Chinna Eranna
Ranch Hand

Joined: Dec 08, 2009
Posts: 174

You expresssion leads to a boolean ....
Switch expression cannot be "Boolean".


- Chinna
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11412
    
  16

assuming i is an int, you could do this:



I wouldn't RECCOMEND it, but it could be done.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

That code is not the same because i < 10 && i > 20 will never be true. A number cannot be both smaller than 10 and larger than 20.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11412
    
  16

Good point. I guess i read it as an 'or'. giving up coffee sucks.
Mahendran Aiyappan
Ranch Hand

Joined: Sep 05, 2005
Posts: 102
Correction please:



Thanks,
Mahendran
Patricia Samuel
Ranch Hand

Joined: Sep 12, 2007
Posts: 300
Why do you want to use switch statement?

Anyways, The answer has already been given by Fred.



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
You could try switch (i / 10) . . ., but that is not exactly what you wrote.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: complex conditions in switch statement is possible?