This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
In cases where there is a long chain of if-else if 's is it better to have it replaced by swich case statements. and also in normal cases, which one of the two is better in terms of performance and memory usage. not to mind the readablity and understanding parameter. Thanks, Chinmay.
The strength of the Wolf is the pack & the strength of the pack is the wolf....Rudyard Kipling
If the comparison for the if statement involves an int or a char variable ,e.g. if(num == 10), ONLY then a switch statement can be subsituted for the if statement. Generally, a switch statement is more efficient than a corresponding if statement. However, you must cater for "fall through" in a switch statement, and you should always include a 'default' case.
I come more from C++, still learning Java, but I rarely see switch statements used. Menus use them. It's not that they're never used, but if you have a large number of cases (or if/elses), you might want to rethink your design, see if you could simplify it. Sometimes arrays are useful here, especially if you're performing the same operation on each case, i.e. updating counters, etc.