C++ Switch Statement

The C++ switch statement is a conditional statement like if-else. It is used to execute a specific choice from all available choices.

The switch statement compares the result of a single expression with all available cases. If the result matches with any case, the relevant block of statements is executed.

Syntax: Switch statement in C++

Switch (expression){
Case 1:
Statements;
Break;
Case 2:
Statements;
Break
.
.
.
Case N;
Statements;
Break
Default:
}
Statements;

Flow Chart:  Switch statement in C++

Example: Switch statement in C++

//Switch Statement in C++
#include <iostream>  
using namespace std;  
int main()  
{  
int x = 4;
    switch (x) {
    case 1:
        printf("You Chose 1");
        break;
    case 2:
        printf("You Chose 2");
        break;
    case 3:
        printf("You Chose 3");
        break;
    case 4:
        printf("You Chose 4");
        break;
    case 5:
        printf("You Chose 5");
        break;
    default:
        printf("Pleace Chose Between 1-5");
        break;
    } 
}

Output

You Chose 4

Recommended Articles


Scroll to Top