How switch statement work in C++ language

The switch is multiple conditional statements in C++ language and further used to execute the code from multiple conditions or case. This is also same as if-else-if ladder statement. This statement work with byte, short, int, char and primitive, enumerated data types and with string also.

Syntax for using switch statement:

switch(expression/variable)
{
case  value-1:   
     //statement-1;
     //statement-2;
     ..
     //statement-n;
     break;  //optional


case  value-2:   
     //statement-1;
     //statement-2;
     ..
     //statement-n;
     break;  //optional
case  value-n:   
     //statement-1;
     //statement-2;
     ..
     //statement-n;
     break;  //optional
default: //optional
     //statements
}

Rules for apply switch statement in C++ language:
  • We can not use float data type switch statement.
  • The expression in switch statement must be of any type like integer or character.
  • The value for a case in a switch case must be same as the variable in switch statement.
  • We use only  byte, short, int, char, enumerated data types and string with switch statement.
  • In a single switch statement, we can use any number of case statements but it can be used only inside the switch statement.

Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: