The conditional statements maintain the flow of a program. They are used in programming for handling decision-making. If-else and switch case are the two conditional statements using which the decisions are made based on the boolean values, true or false. This article will guide you on where to use which conditional statement for obtaining better results.
Also read: Bash ‘if else’ statement examples.
If-else statement
The if-else conditional statement is used to check whether a given condition is true or not. If the condition is true, the ‘if’ block is executed; otherwise the ‘else’ block is executed.
Syntax:
if(condition){
// This block executes when the condition is true
}
else{
//This block executes when the condition is false.
}
You can write if – else if statements or nested if-else statements too, where there are many conditions.
Example:
if(grade == 'A'){
return "Pass! Your grade is A.";
}else if(grade == 'B'){
return "Pass! Your grade is B.";
}else if(grade == 'F'){
return "Fail! You grade is F.";
}else{
return "Result Awaited!";
}
Switch statement
The switch statement is a choice-based statement where you can give various values as cases and an expression. The values of the cases are matched against the value of the expression. The case whose value matches with the expression is executed. When no case matches with the expression, then the default statement is executed.
Syntax:
switch(variable){
case 'A': //code to execute
case 'B': //code to execute
default: //code to execute
}
Example:
switch(grade){
case 'A':
return "Pass! Your grade is A.";
case 'B':
return "Pass! Your grade is B.";
case 'F':
return "Fail! You grade is F.";
default:
return "Result Awaited!";
}
Also read: How to create plots using Pandas?
If-else vs Switch case
If – Else statement | Switch statement | |
---|---|---|
Expressions | The if-else statement supports various expressions values such as conditions or range. | The switch statement’s expression can only have either the integer or character or enumerated value. |
Conditions | There can be multiple conditions in an if-else statement. | The switch statement can have only one condition. |
Speed | The if-else statement works comparatively slow when the number of cases or conditions is more than five. | The switch statement works fast. |
Ideal for | If else conditions can have fixed data value or variables or range. Thus, it is ideal for variable conditions. | Switch statement matches the value of the expression with the fixed data values present for each case. Thus, it is ideal for fixed data values. |
Readability | The if-else statement looks a bit messy as compared to the switch statement. | The switch statement looks clear and clean to read as compared to if-else. |
Jump Table | In the if-else statements, the conditions are checked at runtime only. | The jump table is created during compilation, and while executing the code, the selected case is executed. The jump table makes the switch statement fast. |
Execution | Either the if – block of the code is executed or the else block. Both blocks cannot be executed together. | When the case matches, the matched case, along with all the cases of the switch statement below the matched case, are executed until the ‘break’ statement is encountered or till the end of the switch statement is reached. |
Thus, depending on your problem statement, you can use the switch statement when there are more than five cases, and the fixed data values are present. You can use the if-else statement for situations with multiple conditions or ranges that need to be tested.
Also read: How to stop an Arduino program?