Switch statement in C
Session By
V. Balamurugan, AP/CSE
Switch statememnt in C
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.
Syntax
switch <expr>
{
case constant_1:
{
statements 1;
break;
}
case constant_2:
{
statements 2;
break;
}
default:
{
default statements;
}
}
Facts related to switch
• Duplicate cases are not allowed
Facts related to switch
• Only those expressions are allowed in switch which results in
an integral constant value
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
Facts related to switch
• Variable expressions are not allowed in case labels. Although
macros are allowed
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;
}
}
11
V.BALAMURUGAN AP/CSE
Contact me @ 6383865268
Mail me @balamuruganv@acetcbe.edu.in

SWITCH CASE STATEMENT IN C

  • 1.
    Switch statement inC Session By V. Balamurugan, AP/CSE
  • 2.
  • 3.
    Switch statement • Theswitch() 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.
  • 4.
    Syntax switch <expr> { case constant_1: { statements1; break; } case constant_2: { statements 2; break; } default: { default statements; } }
  • 6.
    Facts related toswitch • Duplicate cases are not allowed
  • 7.
    Facts related toswitch • Only those expressions are allowed in switch which results in an integral constant value
  • 8.
    Facts related toswitch • 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 toswitch • Variable expressions are not allowed in case labels. Although macros are allowed
  • 10.
    Facts related toswitch • 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; } }
  • 11.
    11 V.BALAMURUGAN AP/CSE Contact me@ 6383865268 Mail me @balamuruganv@acetcbe.edu.in