It is a multi-way control statement that almost works as if else statements. The only difference is if else statement work based on condition but with switch case statement it works on matching the switch expression with every case constant. If the matching is found then the corresponding case will be executed. Otherwise the default section will be executed.

Syntax: –

Switch(Variable or Expression)
{
Case label/constant 1:
                Statement 1;
                Break;
Case label/constant 2:
                Statement 2;
                Break;
Case label/constant n:
                Statement n;
                Break;
Default:
                Default statement;
}

Flow chart of switch case: –

flow char of switch case.png

Nested switch case: – It is also possible to define a switch case inside the case of another switch case statement.

Advertisements