Hi everyone, inside this article we will see about C++ switch Statement.
The switch
statement is a control structure in C++ is used to perform multiple actions based on multiple cases. It’s a multi-way branch statement that tests a value against a list of cases, and executes the first matching case.
Syntax & Explanation
The syntax is as follows:
switch (expression) {
case constant-expression :
statement(s);
break;
case constant-expression :
statement(s);
break;
...
default :
statement(s);
}
Where expression
is any expression that returns an integral or enumeration type, constant-expression
is a constant expression of the same type as expression
, and statement(s)
is one or more statements. The break
statement is used to exit the switch
statement. If no break
statement is encountered, the flow of control will fall through to the next case. The default
case is optional and executed when no other case matches.
Here, a pictorial representation of switch
case statement:

A Basic Program
Here is an example of a switch
statement in C++:
#include <iostream> using namespace std; int main() { int day = 3; switch (day) { case 1: cout << "Monday" << endl; break; case 2: cout << "Tuesday" << endl; break; case 3: cout << "Wednesday" << endl; break; case 4: cout << "Thursday" << endl; break; case 5: cout << "Friday" << endl; break; case 6: cout << "Saturday" << endl; break; case 7: cout << "Sunday" << endl; break; default: cout << "Invalid day" << endl; } return 0; }
Output
Wednesday
This program outputs Wednesday
to the console, as day
is equal to 3
.
We hope this article helped you to understand about C++ switch in a very detailed way.
Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.