Difference Between if-else and switch

Computer ProgrammingProgrammingMiscellaneous

In this post, we will understand the difference between if-else statement and ‘switch’ statement.

If-else

  • Depending on the expression inside the statement, output would be generated.

  • It uses multiple statements for multiple choices.

  • This statement tests for equality.

  • It can be used to test logical expressions.

  • It can evaluate integer, character, pointer, floating-point type and boolean type.

  • Just one of the ‘if’ or ‘else’ statement gets executed.

  • If the condition inside the ‘if’ statement is false, then the ‘else’ statement is executed if it has been created.

  • It is tough to edit if-else statement, especially if it is nested.

Switch

  • The statement that needs to be executed is decided by the user.

  • It uses a single expression to work with multiple choices.

  • It only tests for equality.

  • It evaluates a character or an integer value.

  • It is easy to edit, since it can be identified easily.

  • It executes one case after another until a break statement is encountered.

  • The ‘break’ statement terminates the switch statement.

  • If the condition inside the switch statement doesn’t match any case, the default statement is executed if it has been created.

raja
Published on 24-Mar-2021 13:32:46

Advertisements