Difference Between Switch and If else Statement in Tabular form

Share

Switch and if else both are conditional statements (selection statements). These statements control the behaviour of a program with the help of condition evaluation. Both are the basic building block of any language.

The main difference between them is, if else statement evaluates a condition and if the result turns out to be true then if block would be executed otherwise else block. On the other hand, Switch statement evaluates & compares certain expression and executes a particular matched ‘case’ block accordingly.

Difference between Switch and If else Statement in Tabular form :

BasisSWITCH STATEMENTIF-ELSE STATEMENT
DefinitionIt is a control statement that uses single enumerated value to determine which case or statement needs to be executedIt is control statement that evaluates relational expression to determine which block would be executed, i.e if or else
ExpressionIt uses a single integer or enumerated value as its expressionIt uses a relational statement or combination of relational statements as its expression
How It WorksA switch statement checks for equality by matching the value of the expression with the casesIf-statement works differently by evaluating a relational expression to make a decision
Execution SequenceBased on the value, a corresponding case would be selected and executed until a break statement is found.
if no case is matched then default statement is executed
Based on condition evaluation, if true is evaluated then if-block would be executed, otherwise else-block
SpeedIt runs faster than if-statement as it just checks for equalityIt runs slower as it evaluates an expression first and then makes decision
Default ExecutionDefault statement would be executed if no match is foundElse statement would be executed, if the condition is false
EditingIf more choices needed to put in the code, then simply more cases can be appended within a switch statementIf more choices needed to put in, then it requires as much number of If-else statement to be declared as many choices are needed
Expression EvaluationAn expression consisting of integer, character and string type is allowed to be used in a switch statementAn expression consisting of two or more relational expressions having integer type,floating-point type, character type, and boolean types are allowed be used owed to n If statement
EfficiencyA switch statement is more efficient than multiple or nested if statementWhen it comes to use multiple or nested if statement, it makes the code more complex and lengthy
Identical Case or BlockA switch statement cannot have two or more cases with identical valueSuch situation doesn't occur in If

If else Statement (Brief Post: Read Here)

If else statements is a decision-making or selection statement. It uses an expression that gets evaluated  either true or false.

If the condition of If-statement is true then its execution takes place, otherwise, the block of else-statement would be executed. It uses a relational statement or combination of relational expressions.

Syntax:

if(condition)
{
//true block
}
else
{
//false block
}

Program Example: (Language : Java )

public class ConditionalStatementExample {
    public static void main(String[] args) {
        int age = 21;
        if (age >= 18) {
            System.out.println("The Person is eligible for Vote !");

        } else {
            System.out.println("The Person is not eligible for Vote !");
        }
    }
}

Output:

The Person is eligible for Vote !

Explanation:

In the above code, we used a variable named “age” and assigned it with value 21. Further, in if statement, “age >= 18” (21 is greater than 18) expression was evaluated as true . Therefore, if block executed and the program output was “The Person is eligible for Vote !”.

 

Switch Statement (Brief Post: Read Here)

Despite using Nested ifs which make code more complex and lengthy, we can use a switch statement.

A switch statement allows the execution of different parts of code based on the value of an expression.

It first evaluates an expression, and then compares & matches its value against all the cases one by one. When the expression’s value matches a case, it executes the block of that case.

After execution, it complies with the normal flow, if no break statement is encountered. In other words, It would jump to the next case and perform the comparison until it reaches the last case clause. Each case has a unique value or identifier to which the expression gets compared.

If no match is found, then the default block would be executed.

It uses break statement to avoid execution of other cases that comes after the matching case. Each and every case must be different. Lastly, default is optional and executed when all the cases aren’t matched.

Syntax:

switch(expression){    
case val:    
  
 break; 
case val2:    
   
 break;   
......    
    
default:     
 // if nothing matches  
}

Program Example (Java)

public class ConditionalStatementExample {
    public static void main(String[] args) {
        int i = 2;
        switch (i) {
            case 1:
                System.out.println("I am one ");
                break;
            case 2:
                System.out.println("I am two ");
                break;
            case 3:
                System.out.println("I am three ");
                break;
            default:
                System.out.println("nothing is matched ");
        }
    }
}

Output:

I am two 

Explanation: 

In the above code, we used a variable named “i” with value 2.

In the switch statement, the expression was evaluated as 2 and compared with the first case having the value “1”, which didn’t match. Therefore, the case 1 block didn’t execute.

After that, the second case with “2” was compared and matched successfully. As a result, “I am two” was printed and the very next line was break statement.

Therefore, it immediately exited from the switch statement without executing the next cases.

This post was last modified on August 20, 2022

Sandeep Verma

Published by
Sandeep Verma
Tags: difference between if and switch Difference between If-else and Switch difference between switch and if else if statement java