Switch Statement
Let us consider a program segment.
int ch;
printf("Enter a direction code (1-4) : ");
scanf("%d",&ch);
if(ch==1)
{ printf("North");}
else
{
if(ch==2)
{ printf("South");}
else
{
if(ch==3)
{ printf("East");}
else
{
if(ch==4)
{ printf("West");}
else
{ printf("Invalid Direction"); }
}
}
}
it prints one of the String "North","South","East","West" based on the value of integer variable ch if it is in the range of 1 to 4 other wise it prints "Invalid Direction".
The above program segment much ambiguous in remembering the braces starts and its closing. It can be easy rewritten using the switch statement.
int ch;
printf("Enter a direction code (1-4) : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("North");
break;
case 2: printf("North");
break;
Let us consider a program segment.
int ch;
printf("Enter a direction code (1-4) : ");
scanf("%d",&ch);
if(ch==1)
{ printf("North");}
else
{
if(ch==2)
{ printf("South");}
else
{
if(ch==3)
{ printf("East");}
else
{
if(ch==4)
{ printf("West");}
else
{ printf("Invalid Direction"); }
}
}
}
it prints one of the String "North","South","East","West" based on the value of integer variable ch if it is in the range of 1 to 4 other wise it prints "Invalid Direction".
The above program segment much ambiguous in remembering the braces starts and its closing. It can be easy rewritten using the switch statement.
int ch;
printf("Enter a direction code (1-4) : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("North");
break;
case 2: printf("North");
break;
case 3: printf("North");
break;
break;
case 4: printf("North");
break;
break;
default: printf("Invalid Direction.");
}
//===============Statements after switch statement=============
--------------------------------
--------------------------------
The switch statement is composed of multiple case statements followed by value with which variable in the switch is compared and on returning true corresponding case is executed. Break statement is necessary as it transfer the control to out of the switch statement after the execution of a case. if the variable does not math with any value there is optional default: block at the end of switch statement which got executed. there is no need of break statement after the last case though it is default case or some other case if default is not present.
EXAMPLE :
!under construction please re-visit soon for more information
}
//===============Statements after switch statement=============
--------------------------------
--------------------------------
The switch statement is composed of multiple case statements followed by value with which variable in the switch is compared and on returning true corresponding case is executed. Break statement is necessary as it transfer the control to out of the switch statement after the execution of a case. if the variable does not math with any value there is optional default: block at the end of switch statement which got executed. there is no need of break statement after the last case though it is default case or some other case if default is not present.
EXAMPLE :
!under construction please re-visit soon for more information