3. Switch statement
• The switch() case statement is like the if statement
that allows us to make a decision from a number
of choices.
• The switch statement requires only one argument
of any data type, which is checked with a number
of case options.
• The switch statement evaluates the expression
and then looks for its value among case constants.
• If the value matches with a case constant, this
particular case statement is executed.
• If not, the default is executed.
7. Facts related to switch
• Only those expressions are allowed in switch which results in
an integral constant value
8. Facts related to switch
• Float value is not allowed as a constant value in case label.
Only integer constants/ constant expressions are allowed in
case label
9. Facts related to switch
• Variable expressions are not allowed in case labels. Although
macros are allowed
10. Facts related to switch
• Default can be placed anywhere inside the switch. It will still
get evaluated if no match is found
int main()
{
int x=2;
switch(x){
default: printf(“default case”);
break;
case 1: printf(“Number is 1”);
break;
case 2: printf(“Number is 2”);
break;
}
}