Tuesday, March 25, 2008

Difference in use of switch and If blocks

If we want to loop on many conditions for a different result for different input, then we will go for either switch or if else ladder. In performance point of view switch can be considered as the best option to choose. But we must take care of variable declarations. In if we can declare a variable for a every condition with same name. Because it's scope will be up to the condition braces only. But in switch it is not like that. whole switch is considered as a block and scope of variable will be alive.

No comments: