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 Avoiding if else loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Avoiding if else loop" Watch "Avoiding if else loop" New topic

Avoiding if else loop

Suhem Programmer

Joined: Feb 13, 2007
Posts: 21

I would appreciate it if some one could suggest me what to use insted of if else loops in the programme.
I just know that useing number of if-else loop in the method is not a good practise but. What to replace it with?
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13036
Before jumping to any conclusions about what is good practice - how about a little more information about your problem.

1. how many different comparisons do you need? If just two or three "if" statements are needed, there is no reason to get more complicated.

2. how complex is the logic in the "if"

Suhem Programmer

Joined: Feb 13, 2007
Posts: 21
Hi Bill,
Thanks a lot for the reply.
There are more than 15 conditions I need to check in the my code.
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Sometimes it is possible to create a map. A simple lookup would return the value if there is a key match.

SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Sometimes you can use polymorphism and interfaces.

Here's a simple example. Below is a class that calculates a discount;

I've only put three if-elses in this class, but imagine there are 15 or 20. The code would look pretty ugly with that many.

Now suppose I create the following interface and implementations of the interface:

Now my SalesManager class looks like this:

Granted, the refactored version actually has more lines of code, but it's a more elegant solution. It's easier for someone to figure out what the calculation of customer type "B" is, for example. Again, it's a judgement call. If I really only had 3 if-elses then I might not go to the trouble of refactoring. But if I had 3 now, and knew it might expand to many more, I'd definitely choose the 2nd method.

Consultant, Sima Solutions
Suhem Programmer

Joined: Feb 13, 2007
Posts: 21
Hi Merrill,
Thanks a lot for the solution! I really appreciate your time. I think I should go with the second solution which you explained in your blog.

Suhem Programmer

Joined: Feb 13, 2007
Posts: 21
Hi Roger,

Thanks a lot for the reply! yeah I had read somewhere online about using collection as well.

Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
I'm not even sure what an "if else loop" is. An if/else statement is not a loop. What exactly is it that you're trying to avoid?

"I'm not back." - Bill Harding, Twister
An Sush
Ranch Hand

Joined: Jan 17, 2007
Posts: 47
You can also use conditional operator, ? : opertaor to good use.
Suppose I have a logic like

you can substitute it with:

You can use it if you have one single if-else logic.
I agree. Here's the link:
subject: Avoiding if else loop
It's not a secret anymore!