C++

Providing An Example, The Switch Case Statement In C++

When there are several circumstances and we need to take a different action depending on the condition, we utilize a switch case statement. when a condition is satisfied and there are numerous conditions, we need to execute a block of statements. In this situation, either a lengthy if..else-if statement or a switch case can be used. Long if..else.if statements have the drawback of becoming complicated when there are multiple criteria. The switch case is a neat and effective way to handle such circumstances.

The syntax of Switch case statement:

switch (variable or an integer expression)
{
     case constant:
     //C++ code
     ;
     case constant:
     //C++ code
     ;
     default:
     //C++ code
     ;
}

Even though the break statement is optional, switch case statements are frequently used with them. First, we’ll look at a scenario without a break statement, and then we’ll talk about switch cases with breaks.

Case study of a Switch

#include <iostream>
using namespace std;
int main(){
   int num=5;
   switch(num+2) {
      case 1: 
        cout<<"Case1: Value is: "<<num<<endl;
      case 2: 
        cout<<"Case2: Value is: "<<num<<endl;
      case 3: 
        cout<<"Case3: Value is: "<<num<<endl;
      default: 
        cout<<"Default: Value is: "<<num<<endl;
   }
   return 0;
}

Output:

Default: Value is: 5

Explanation: I used an expression in the switch, but you can use a variable instead. I entered the phrase num+2, where num equals 5, and the result of addition was 7. The default case was used because there was no case defined with value 4.

Diagram of the Switch Case Flow

It first determines whether an expression or variable is true (using the information provided inside switch brackets), and then, depending on the result, it runs the appropriate case.

Statement Break in the Switch Case

Before we talk about the break statement, let’s look at what occurs in the switch situation when we don’t utilize the break statement. See the illustration below:

#include <iostream>
using namespace std;
int main(){
   int i=2;
   switch(i) {
      case 1: cout<<"Case1 "<<endl;
      case 2: cout<<"Case2 "<<endl;
      case 3: cout<<"Case3 "<<endl;
      case 4: cout<<"Case4 "<<endl;
      default: cout<<"Default "<<endl; 
   }
   return 0;
}

Output:

Case2 
Case3 
Case4 
Default

In the program above, the variable I is enclosed in switch braces, meaning that regardless of its value, the associated case block is run. Due to the integer value 2 that we gave to the switch, the control was transferred to case 2, but since there was no break statement after case 2, the flow continued to the remaining cases until the end. But this was not our intention; instead, we wanted to execute the proper case block and disregard the remaining blocks. The break statement in the after every case block is the answer to this problem.

When your program flow should leave the switch body, utilize a break statement. Anytime a break statement is found in

When you wish your program’s flow to leave the switch body, you use break statements. When a break statement appears in the switch body, the execution flow will bypass the remaining cases and exit the switch. For this reason, a break statement must be used to stop each case block.

Take the same example, but add a break statement this time.

#include <iostream>
using namespace std;
int main(){
   int i=2;
   switch(i) {
      case 1:
        cout<<"Case1 "<<endl;
        break;
      case 2:
        cout<<"Case2 "<<endl;
        break;
      case 3:
        cout<<"Case3 "<<endl;
        break;
      case 4:
        cout<<"Case4 "<<endl;
        break;
      default:
        cout<<"Default "<<endl;
    }
    return 0;
}

Output:

Case2

You can now see that only case 2 was carried out, with the remaining cases being disregarded.

Why did I not utilize the break statement following default?

I didn’t use a break statement after default because the control will exit the switch on its own; however, you can if you choose; there is no risk in doing so.

Important Remarks

1) The sequence of the cases does not always have to be 1, 2, 3, etc. After the case keyword, it can have any integer value. Additionally, the sequence of the cases need not always be ascending; you can define them in any order depending on the situation.

2) Switch case characters are also acceptable. for instance –

#include <iostream>
using namespace std;
int main(){
   char ch='b';
   switch(ch) {
      case 'd': cout<<"Case1 ";
      break;
      case 'b': cout<<"Case2 ";
      break;
      case 'x': cout<<"Case3 ";
      break;
      case 'y': cout<<"Case4 ";
      break;
      default: cout<<"Default ";
   }
   return 0;
}

3) Switch statements may be nested, meaning that one switch may be nested inside of another. Nested switch statements should be avoided, nevertheless, as they add complexity and make programs harder to read.

Related Articles

Leave a Reply

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

Back to top button