Computer Applications
Answer
Following are the difference between if and switch statement:
- switch can only test for equality whereas if can test for any Boolean expression.
- switch tests the same expression against constant values while if-else-if ladder can use different expression involving unrelated variables.
- switch expression must only evaluate to byte, short, int, char, String or an enum. if doesn’t have such limitations.
- A switch statement will run much faster than the equivalent program written using the if-else-if ladder