C switch case

  • Switch is a keyword by using using Switch we can create selection statement with multiple options.
  • Multiple blocks can be constructed by using case keyword.
  • When we are working with the switch it required expression or condition as type on integral only.

Syntax:

switch(condition)
{
case const1:
Block1;
break;
case const2:
Block2;
break;
case const3:
Block3;
break;
——–
——–
——–
default: Block;
}

  • At the time of execution if matching case is occur then control will pass to correspondent block, from matching case upto break everything will be executed, if break is not occurred then including default all cases will be executed.
  • At the time of execution if matching case is not occur then control will pass to default block.
  • Default is a special kind of case which will executed automatically when matching case is not occur.
  • Using default is always optional, it is recommended to use when we are not handling all cases of the Switch block.

Examples:

void main()
{
int i;
i=2;
switch(i)
{
case 1:printf(“One”);
case 2:printf(“Two”);
case 3:printf(“Three”);
case 4:printf(“Four”);
default:printf(“Zero”);
}

Powered by k2schools