“If-else” and “switch” both are selection statements. The selection statements, transfer the flow of the program to the particular block of statements based upon whether the condition is “true” or “false”.
The fundamental difference between if-else and switch statements is that the if-else statement “selects the execution of the statements based upon the evaluation of the expression in if statements”. The switch statements “selects the execution of the statement often according to a keyboard command”.
Content: if-else Vs switch
Comparison Chart
Basis for Comparison | if-else | switch |
---|---|---|
Basic | Which statement will be executed depend upon the output of the expression inside if statement. | Which statement will be executed is decided by user. |
Expression | if-else statement uses multiple statement for multiple choices. | switch statement uses single expression for multiple choices. |
Testing | if-else statement test for equality as well as for logical expression. | switch statement test only for equality. |
Evaluation | if statement evaluates integer, character, pointer or floating-point type or boolean type. | switch statement evaluates only character or integer value. |
Sequence of Execution | Either if statement will be executed or else statement is executed. | switch statement execute one case after another till a break statement is appeared or the end of switch statement is reached. |
Default Execution | If the condition inside if statements is false, then by default the else statement is executed if created. | If the condition inside switch statements does not match with any of cases, for that instance the default statements is executed if created. |
Editing | It is difficult to edit the if-else statement, if the nested if-else statement is used. | It is easy to edit switch cases as, they are recognized easily. |
Definition of if-else
The if-else statements belong to selection statements in OOP. The general form of the if-else statements is as follow
if (expression){ statement(s) }else{ statement(s) }
where “if” and “else” are the keywords, and the statements can be a single statement or a block of statements. The expression evaluates to be “true” for any non-zero value and for zero it evaluates to be “false”.
The expression in if statement can contain an integer, character, pointer, floating-point or it can be a boolean type. The else statement is optional in an if-else statement. If the expression returns true, the statements inside if statement is executed, and if it returns false the statements inside else statement are executed and, in case an else statement is a not created no action is performed, and the control of the program jump out of an if-else statement.
Example
Lets us understand with an example.
int i=45, j=34; if (i==45 & j==34){ cout<< " i ="<<i; }else{ cout<< " j ="<<j; } //output i=45
Here, the if statement evaluates the equality along with the logical evaluation. If the expression would have return false, then the statement in an else statement would have been executed.
Definition of switch
The switch statements is a multiple-choice selection statement. The general form of the switch statement is as follow
switch( expression ){ case constant1: statement(s); break; case constant2: statement(s); break; case constant3: statement(s); break; . . default statement(s); }
Where the expression evaluates an integer or character constants. The expression here only evaluates for equality. The expression is verified against the constants present in the case statements. If a match is found, the statements associated with that case is executed, until a “break” occurs. As the break statement is optional in the case statements, if the break statement is not present then, the execution does not stop until the end of the switch statement.
The expression only contains a single expression. The switch statement often uses a keyboard command to choose one among the multiple case statements.
int c; cout<<" choose the value from 1 to 3"; cin>>i; switch( i ){ case 1: cout<<"you choose dark choclate"; break; case 2: cout<<"you choose candy"; break; case 3: cout<<"you choose lollypop"; break; . . default cout<<"you choose nothing"; }
Here, the value of “i” will decide which case is to be executed, if a user gives the value of “i” other than 1, 2, or 3, then the default case is executed.
Key Differences Between if-else and switch
- The expression inside if statement decides whether to execute the statements inside if block or under else block. On the other hand, the expression inside a switch statement decides which case to execute.
- You can have multiple if statement for multiple choice of statements. In switch, you only have one expression for the multiple choices.
- If-else statement checks for equality as well as for logical expression. On the other hand, switch checks only for equality.
- The if statement evaluates integer, character, pointer or floating-point type or boolean type. On the other hand, switch statement evaluates only character or an integer datatype.
- Sequence of execution is like either statement under if block will execute or statements under else block statement will execute. On the other hand, the expression in switch statement decide which case to execute and if you do not apply a break statement after each case it will execute till the end of the switch statement.
- If expression inside if turn outs to be false, statement inside else block will be executed. If expression inside switch statement turns out to be false then default statements is executed.
- It is difficult to edit if-else statements as it is tedious to trace where the correction is required. On the other hand, it is easy to edit switch statements as they are easy to trace.
Conclusion
The switch statement is easy to edit as it has created the separate cases for different statements whereas, in nested if-else statements it becomes difficult to identify the statements to be edited.
Lekhram sahu says
Thank you so much.
Hariharan says
Very useful
Wajahat♡Jatt says
I got 10/10 marks with the help of it
BOGDAN says
Very nice!
BIG THANK YOU!
SATISH M N says
Definition of if-else
The if-else statements belong to selection statements in OOP
Can’t it be used in POP?
amankothari says
best of the best
Shafaque says
Thanx alot,it is really very helpful 🙂
Saro Amani says
Thanks a lot
it was very useful.
Sheharyar Noor says
GOOD EXPLANATION 😉
ThomasPP says
Thank you ! Very well made !
roy says
very helpfull information