A Switch is a control statement that allows value to change control of execution.
These are the fact about the switch statement.
- Switch case statement expressions must be of integral type (int,char and enum). Any other type of expressions used in switch case are not allowed.
- Until a break statement is encountered ,all the above matching statements get executed.
- We can place the default block anywhere .
- The labels used by integral expression should be a constant expressions.
- The statement of switch cases written above are never executed.
- The same values should not consist in any two case labels.
Simple Program to explain the syntax of switch
#include
int main()
{
int m=2;
switch(m)
{
case 1: printf(Color is 1);
break;
case 2: printf(Color is 2);
break;
case 3: printf(Color is 3);
break;
default: printf(Color other than 1, 2 and 3);
break;
}
return );
}
Output for the above program
Color is 2
0 Comment(s)