This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes How to avoid multiple if-else if(say 25 conditions)? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to avoid multiple if-else if(say 25 conditions)?" Watch "How to avoid multiple if-else if(say 25 conditions)?" New topic
Author

How to avoid multiple if-else if(say 25 conditions)?

Senthil Kumar Sekar
Ranch Hand

Joined: Aug 08, 2010
Posts: 45

Hi,
I have say 25 if-else if conditions.So,should i have only 25 if-else if conditions.
Is there any better/effective way of checking it?
The disadvantage i see here is, if the condition satisfies only at 25th condition, it needs to check unnecessarily for 24 conditions.

PS : if-else if check i mean here can be anything (From String literal to an Object).


Please suggest.

Thanks.

Regards
Senthil Kumar Sekar
john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495


Is that what you were looking for? Maybe a while statement?
cc11rocks

EDIT : The poster below has a better solution. Java 7 comes out July 28, 2011 FYI


“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” (Mosher's Law of Software Engineering)
“If debugging is the process of removing bugs, then programming must be the process of putting them in.” (Edsger Dijkstra)
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

Looks like you want a powerful SWITCH that accepts non integers too. I wanted it too. This is what is available as of now : In Java SE 7 and later, you can use a String object in the switch statement's expression.
See - http://download.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

Right now , I am trying to make code that will take objects too. I failed !

SCJP 6. Learning more now.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Another solution might be to use a Map. I don't know exactly what expressions you have in your if-statements, so I can't tell you if this will work for your situation. But suppose that your chain of if-statements looks something like this:

You could do that with a Map like this:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

The disadvantage i see here is, if the condition satisfies only at 25th condition, it needs to check unnecessarily for 24 conditions.

Your question might be of academic intrest but I seriously doubt the 24 checks would result in a noticeable performance lag.

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to avoid multiple if-else if(say 25 conditions)?
 
Similar Threads
Help with coding
AND operator not working as desired in XSL
Is this MVC?
boolean && || vs & | precedence
Question re: nested IF statements