switch-case or if-else ?

While this is a personal choice for a developer, I found while writing applications, sometimes we just need to go ahead with a simple one conditional if-else and finish it. But imagine if we have a number of conditions to check and need to know actually where it all is going wrong. In this case probably we can write something as

trace(‘This is inside ELSE,resultNumber=’,resultNumber);

In the above case, if we need to check for different “correctNumber” values at different time, we may go ahead and put similar IF statements with an accompanying ELSE for each of the IFs. While everything is fine, I personally found using SWITCH-CASE in this kind of scenarios reduces code and puts us right in the debugging mode from the beginning.

case 45 :
case 60 :

default :
trace(‘This is DEFAULT case and not handled yet!! correctNumber=’,correctNumber);

As you can see, any case not handled, will trace out the default block. Much convinient and sweet :) As I was thinking for a lot of time,what could be the exact use of a “default” case in a switch-case statement, I found my answer in this kind of scenarios.

Writing a SWITCH-CASE and always putting a DEFAULT case with the case not handled in the trace output, is always a handy thing to do for the code which will be handling a lot of coparisions.

Hope that helps someone out there.

Happy Flashing :)


3 thoughts on “switch-case or if-else ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s