Switch Statement in C++

Filed Under: C++

The Switch Statement in C++ acts as a control statement and select a particular matching condition from a set of conditions.

These statements are an easy substitute to the monotonous if-else statements.

It selects one block with the matching case-label out of all the blocks in the code.

Switch statements are helpful when we have a set of multiple conditions and the user needs to select one of them based on the matching condition.

Syntax:

switch(expression/condition) {
  case constant_value1:
    // statements
    break;
  case constant_value2:
    // statements
    break;
   .
   .
  case constant_valueN:
    //statements
    break;
  default:
    // statements
}

Working of Switch Statement in C++

Switch Case
Switch Case

The switch statement works upon the expression input by the user to check for a match.

As soon as an expression is encountered by the compiler, it searches for a matching value of the expression with the case labels.

If a match is found, the piece of code within that particular block associated with the matched case label is executed.

Thus, the switch statement is taken into consideration only once upon the match to be found.

Significance of “break” keyword

As soon as a match is found with the case label, the break statement gets executed.

The break statements terminate the execution of the code in the block, following the statement and get the execution point out of the switch statement.

Thus, it neglects and terminates the execution of the rest of the code and comes out of the switch statement.

Significance of “default” keyword

The default keyword is responsible for executing the piece of code if no matching case labels are found.

Choosing data type for the case labels in switch statements

  • The data type of case labels of the switch statement in C++ can be either an integer or character type.
  • The case label can be either a constant variable or a constant expression.

Points to Remember

  • The case label can be of integer, short, long or character data type.
  • Switch statements do not support float or double type case labels.
  • The data type of the expression and the case label needs to be the same.
  • The case label cannot be an expression or a variable. It can be an integer value such as 1, 2, 3, so on or a character such as ‘A’, ‘w’, so on.
  • Duplicate case label values turn out as error and are not allowed.
  • The default statement is completely optional and doesn’t affect the flow of execution of switch cases.
  • Any statement that is written above the switch case labels never gets executed.
  • The position of the default statement does not affect the execution and can be placed anywhere within the code.

Understanding switch case in c++

Example:

# include <iostream>
using namespace std;
int main()
{
    char choice;
    float input1, input2;
    cout << "Enter the input values:\n";
    cin >> input1;
    cin >> input2;
    cout << "Choose the operation to be performed from (+, -, *, /) : ";
    cin >> choice;
    switch(choice) #expression
    {
        case '+': # case-label
            cout << "Performing Addition operation on the input values..\n";
            cout << input1+input2;
            break;
            
        case '-':
            cout << "Performing Subtraction operation on the input values..\n";
            cout << input1-input2;
            break;
            
        case '*':
            cout << "Performing Multiplication operation on the input values..\n";
            cout << input1*input2;
            break;
            
        case '/':
            cout << "Performing Division operation on the input values..\n";
            cout << input1/input2;
            break;
            
        default:
            
            cout << "Choose the correct operation to be performed.";
           
    }
    return 0;
}

Output:

Enter the input values:
10
10
Choose the operation to be performed from (+, -, *, /) : /
Performing Division operation on the input values..
1

In the above code snippet, the input operation is division (/). So, the expression (input) checks for the matching case label.

As soon as a match is encountered, the flow of control execution shifts to the particular block and the division operation is performed.


Conclusion

Thus, in this article, we have understood the working of Switch Statements in the C++ language.


Reference

C++ Switch Case Documentation

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages