Explain the syntax and working of the switch case statement. which situation is desirable.

03 a] Explain the syntax and working of the switch case statement. Mention in which situation it is desirable.

Answer:-

Syntax of Switch Case Statement:-

switch(choice)
{
case label1: block1;
break;
case label2: block2;
break;
case label3: block-3;
break;
default : default-block;
break;
}

  • Here switch, case, break and default are built-in C language words.
  • If the choice matches label 1 then block 1 will be executed else if it evaluates to label 2 then block 2 will be executed and so on.
  • If the choice does not match any case labels, then the default block will be executed.

Working of Switch case statement

Flow chart for a Switch case statement

➢ The choice is an integer expression or characters.
➢ Label1, label2, label3,…. are constants or constant expressions evaluated to integer constants.
➢ Each of these case labels should be unique within the switch statement. block1, block2, block3, … are statement lists and may contain zero or more statements.


➢ There is no need to put braces around these blocks. Note that case labels end with a colon(:).
➢ Break statement at the end of each block signals end of a particular case and causes an exit from a switch statement.
➢ The default is an optional case when present, it will execute if the value of the choice does not match any of the case labels.

C language provides a multi-way decision statement so that complex else-if statements can be easily replaced by it.

C language’s multi-way decision statement is called a switch.

Leave a Reply

Your email address will not be published. Required fields are marked *