C++ switch statement

C++ switch statement

switch is multi way conditional control statement. which allow to execute multiple operations for the different values.it help us to make choice on the number of alternatives.

Here the syntax of switch statement is

switch(expression)
{
case constant1:
statement
………
case constant2:
statement
………
………
case constantn:
statement
………
default:
statement
………
}

in the above example switch, case and default are keywords. The expression following in the switch keyword can be integer or character expression. Then the constant expression in the case label should be of character or integer type. Every constant should be different from another (each case value should be different or unique) and we can not use float or string constant in case. multiple constant in a single case not allowed only one constant allow. In each case followed by any number of statements. The statements under any case can be any valid C statements like for, do-while, while, if-else etc or can give another switch statement inside switch, declare another switch statement inside switch statement is call nesting of switches

Let us take example how switch case works

/*program to understand the switch statement*/
#include<iostream>
int main()
{
int choice;
std::cout<<"Enter any choice 1,2,3";
std::cin>>choice;
switch (choice)
{
case 1:
std::cout<<"Choice is one";
break ; //optional
case 2:
std::cout<<"choice is two";
break ; //optional
case 3:
std::cout<<"choice is three";
break ; //optional
default : //optional
std::cout<<"wrong choice";
}
return 0;
}

let us discuss how switch statement works

firstly the switch control statement expression evaluated. if the expression value is matched with the case constant expression. Then the control transfer to the case label. but if the any case label not match with switch expression then control is transfer to the default label. The default label is optional. but in this example break keyword are used. break keyword is used to bring the program control out of the loop or block. so here is the example after execute any case label break keyword jump to the out of the switch statement. The break keyword can be use inside any loop, block etc

int a,b;
float x,y;
char ch;

Valid Switch Invalid switch valid case invalid case
switch(a) switch(x) case 10 case 12.9
switch(a+b) switch(x-2.8) case ‘A’ case a
  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise