Switch Statements:

A switch statement is a control flow statement found in many programming languages. It provides a way to select and execute different code blocks based on the value of a given expression.


switch(expression) {
case x:
    // code block
    break;
case y:
    // code block
    break;
default:
    // code block
}

Description:

1. The switch statement begins with the keyword switch, followed by an expression enclosed in parentheses. This expression is often referred to as the "selector" or "switch variable.".
2. After the opening curly brace. you define individual cases using the case keyword, followed by a constant value or expression that represents a specific case to be matched.
3. Each case block consists of one or more statements that will be executed when the corresponding case matches the value of the selector expression. The statements within each case block are typically indented for readability.
4. At the end of each case, it's essential to include the break statement to exit the switch block. Without break statement, the execution will continue to the next case, which may not be the desired behavior.
5. Optionally, you can include a default case, which is executed when none of the previous cases match the value of the selector expression. It acts as a fallback option when no specific case matches.
6. The closing curly brace marks the end of the switch statement.


switch.cpp
#include<iostream>
using namespace std;

int main(){
    int num;
    cout<<”Enter an integer from 1 to 3;
    cin>>num;
    switch(num){
    case 1:
    	cout<<”a is equal to 1<<endl;
    	break;
    case 2:
    	cout<<”a is equal to 2<<endl;
    	break;
    case 3:
    	cout<<”a is equal to 3<<endl;
    	break;
    default:
    	cout<<”a is not in range from 1 to 5<<endl;
    	break;
    }
    return 0;
}

Note:
Do not forget to put break after every case.
Default case is optional. It is executed when no case is executed. There is no need to add a break after default case.