Switch case in C | Conditional statements |C programming | C concepts

             SWITCH CASE:

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 ;

 }


POINTS TO REMEMBER:

  • 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:



  1. In case of Switch case,duplicate cases are not allowed.

  2. We can’t use an real number in case of Switch case.

  3. 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.

  4. 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.



Now we will see an example of Switch case that how switch case is implemented.

Q.write a program in C to input any no number between 1 to 7 and the corresponding day will be presented of the week.



output:

Explanation:
Here we take any integer as an ouput. And the integer value is stored in variable 'a' .
Enter a number between 1 to 7 = 4
a=4;
case 4 will be executed then break statement.
Break is used to move out of the Switch block.Whenever break is found rest part of the program in switch block get skipped.

output: Wednesday.


Hope you guys like this. For any queries related to this feel free to ask us in the comment section.You can also contact us through comment section. If you like it please subscribe this let us know in comment section.
Thank You!












Comments