C++ Switch Statements

Hey! Guys, we will learn C++ Switch Statements today and will learn switch case c++, break and default statements/keywords.

The C++ Switch Statement

We are using a switch statement to select one of these multiple statements that are executing at the same time. So, the Syntax of the switch statement is explaining below…

switch(expression)
{
   case a:
    // the block of code
    break;
   case b:
    // the block of code
    break;
   default:
    // the block of code
}

So, Now I am telling you how it work

  • In the 1st, the switch statement takes the expression
  • Then, the second work is a comparison of each value of cases between expression and case
  • So, the third method is: if the value of an expression is matching with any value of a case. then the particular code of block will be executed

The Switch statement is easy because We are explaining a complete brief example in the below:

#include <iostream>
using namespace std;

int main()
{
     int month = 4;
     switch(month)
     {
         case 1:
           cout << "January";
           break;
         case 2:
           cout << "February";
           break;
         case 3:
           cout << "March";
           break;
         case 4:
           cout << "April";
           break;
         case 5:
           cout << "May";
           break;
         case 6:
           cout << "June";
           break;
         case 7:
           cout << "July";
           break;
         case 8:
           cout << "August";
           break;
         case 9:
           cout << "September";
           break;
         case 10:
           cout << "October";
           break;
         case 11:
           cout << "November";
           break;
         case 12:
           cout << "December";
           break;
     }
}

The output will “April

The break statement/keyword

When C++ reaches the break keyword, Then it breaks out the switch statement. The case testing the value inside the block of code. When the case found that the is the match. it stops the execution and job is done, and no need to test more block of code.

The default keyword

So, the default keyword is running a specified block of code, when the case is no match with every number of cases. The Example:

#include <iostream>
using namespace std;

int main()
{
     int noAndOf;
     cin >> noAndOf;
     switch(noAndOf)
     {
         case 0:
           cout << "Light is Off";
           break;
         case 1:
           cout << "Light is On";
           break;
         default:
           cout << "Please enter Only 0 or 1";
     }
}

The output is:

3
Please enter Only 0 or 1

Remember: Always use default keyword in the end of switch statement and it does not need the break statement.

I hope you are enjoying this course for free, please post your feedback in the comment section below. Go to earlier Chapter of this course and the chapters is below:

Some Recent chapters:

Thanks for Support us…

By Usama Arshad

It's me Usama Arshad working as a Lecturer C++. I am a student of BSIT.

Leave a Reply

Your email address will not be published. Required fields are marked *