KnowledgeBoat Logo

Computer Applications

Differentiate between if and switch statement.

Java Conditional Stmts

ICSE 2005

134 Likes

Answer

Following are the difference between if and switch statement:

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

Answered By

76 Likes


Related Questions