Switch Statement in C++

In C++, the switch statement is used when multiple situations need to be evaluated primarily based on the value of a variable or an expression. switch statement acts as an alternative to multiple if statements or if-else ladder and has a cleaner structure and it is easy for handling multiple conditions.

Syntax of C++ switch

switch (expression) {
case value1:
// code to be executed if the value of expression is value1.
break;
case value2:
// code to be executed if the value of expression is value2.
break;
//…..
default:
// code to be executed if expression doesn't match any case.
}

Flowchart of switch in C++

Flowchart of switch in C++

Example pf switch in C++

The below example demonstrates the use of switches in decision-making. In the below program, we are given a character and you have to give output as per the given condition: if the given input is A then print GFG, and if the given input is B print w3wiki else print invalid input.

C++




// C++ program to use switch case and print certain output
// based on some conditions
  
#include <iostream>
using namespace std;
  
int main()
{
    char input = 'B';
    switch (input) {
    // if the input character is A then print GFG
    case 'A':
        cout << "GFG" << endl;
        break;
  
    // if the input character is B then print w3wiki
    case 'B':
        cout << "w3wiki" << endl;
        break;
    default:
        // if th einput character is invalid then print
        // invalid input
        cout << "invalid input" << endl;
    }
    return 0;
}


Output

w3wiki

Decision Making in C++

Decision-making in C++ involves the usage of conditional statements (also called decision control statements) to execute specific blocks of code primarily based on given situations and their results.

So basically, in decision-making, we evaluate the conditions and make a decision about which part of the code should be executed or not. It allows selective code execution which is crucial for controlling the flow of a program and making it more dynamic.

 

Similar Reads

Types of Decision-Making Statements in C++

In C++, the following decision-making statements are available:...

1. if in C++

In C++, the if statement is the simplest decision-making statement. It allows the execution of a block of code if the given condition is true. The body of the ‘if’ statement is executed only if the given condition is true....

2. if-else in C++

...

3. if-else if Ladder in C++

The if-else decision-making statement allows us to make a decision based on the evaluation of a given condition. If the given condition evaluates to true then the code inside the ‘if’ block is executed and in case the condition is false, the code inside the ‘else’ block is executed....

4. Nested if-else in C++

...

5. Switch Statement in C++

The if-else-if statements allow us to include additional situations after the preliminary if condition. The ‘else if’ condition is checked only if the above condition is not true. , and the `else` is the statement that will be executed if none of the above conditions is true. If some condition is true, then no only the associated block is executed....

6. Ternary Operator ( ? : ) in C++

...

7. Jump Statements in C++

The Nested if-else statement contains an ‘if’ statement inside another ‘if’ statement. This structure lets in more complex selection-making by way of comparing multiple conditions. In this type of statement, multiple conditions are checked, and then the body of the last if statement is executed....

Conclusion

...