
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between If-Else and Switch
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.