Switch case is a conditional statement in C.It is a multi-way decision process.It gives multiple options ,out of which one has to be selected .
It is always an alternative to if else condition.
Syntax:
Switch(Condition / Expression)
{
Case lable1:
……………
……………..
Break ;
Case lable 2:
…………………
………………….
Break ;
Case lable 3:
……………
……………..
Break ;
Deafault :
……………
………………
Break ;
}
Condition or expression in switch is compulsory.
We cannot have any real data, only integers and characters are allowed.
All the labels will be either an integer data or a character data .
No variables are allowed .
So ,default block is optional and break is also optional .
Inside switch block no of cases are optional also.
PRECAUTIONS TO BE TAKEN IN SWITCH CASE:
In case of Switch case,duplicate cases are not allowed.
We can’t use an real number in case of Switch case.
Break is an optional part.If we do not use break.Then,from the matching case all statements will execute until a break is found or closing curly braces is found.
We can write the case statement in any order i.e. it is not mandatory to write down case 2 after case 1.It is applicable to switch case having break only.
Comments
Post a Comment