Switch case in C++
-
The control statement that allows us to make a decision from the number of choices is called a switch, or more correctly a switch-case-default.
The Switch executes the case where a match is found and then all subsequent cases till end, so stop that fall through we use break; statement in each case.
-
A switch works with the integral or enumerated types.
Syntax :
switch(expression){ case value1 : statement(s); break; //optional case value2 : statement(s); break; //optional ..... /* you can have any number of case statements. */ default : //Optional statement(s); /* code to be executed if all cases are not matched */ }
Block Diagram :

Example:
WAP to give choice to user from menu to perform action.#include<iostream> using namespace std; int main( ) { int x ; cout<< "MENU\n----------\n1. for Hello\n2. for Bye\n-----------\n"; cout<< "Enter Your Choice: "; cin>>x; switch(x) { case 1: cout<< "HELLO USER!!\n"; break; case 2: cout<< "BYE USER!!\n"; break; default: cout<<"INVALID!!\n"; } return 0; }OUTPUT:
MENU ----------- 1. for Hello 2. for Bye ----------- Enter Your Choice: 1 HELLO USER!!
Next topic is Loops
Training For College Campus
We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies
like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.
Please mail your requirement at info@prowessapps.in
Projects For Students
Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.
Students can mail requirement at info@prowessapps.in