Hi, Even as performance wise, I would say switch is the better option, 'coz switch statements will have break statements which can rule out the other possibilities. But for u, I don't know whether this will help or not because u have multiple true cases. Regards Nayan.
It would, of course, depend on your application and the data (rule of thumb KYD. Know Your Data)and break it down into sections where the cases could be multiples. If multiple options can be taken, then you may have to use IF blocks to get better control of the flow. No, it isn't pretty, but 100+ lines of case/break statements tends to be messy as well. My sugestion would be to use IF logic to group the incomming data with a specific set of "cases" then use a switch to get the exact option you need at the time. You should actually be able to better streamline your code by doing this. Just because you have the ability to use a "slick" trick (like Switch / Case) to make something run or look cool doesn't mean it will work in all cases. Especially when you have to MAINTAIN the code at a later date (or worse, someone else has to do it... good way to make enemies ). Hope this helps... [ January 09, 2003: Message edited by: Sam Smoot ]
If you really have hundreds of cases, possibly neither if nor switch are the best solution. Can you give more details on what you want to accomplish?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I think that readability is usually more important than performance, and switch usually wins here, IMO. And if we only consider differences in performance between these two: FYI, in many cases a switch statement gets implemented by the compiler as a series of if statements, organized to be pretty efficient. You may be able to do better by hand, but often not. In many cases the compiler uses a jump table, which can be substantially more efficient than a series of if statements when there's a large number of options. In some cases hand-coded if statements can be more efficient than what the compiler will create for you from a switch - particularly if one of the options is much more common than the others; you can test for that first and save time. (It's possible Hotspot can also optimize this sort of thing; dunno.) Usually I'd be pretty confident in the compiler's ability to generate efficient code from a switch statement.