switch statement c++ example program

Example 1: how to make a switch case statement in c++

#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;
}

Example 2: c++ switch case

switch(expr) {
  case 1:
    // do something
    break;
  case 2:
    // do something
    break;
  default:
    // do something
}

Example 3: switch in c++

switch (variable) {
  case 1:
    // code here
    break
  default:
    // code here 
    break
}

Tags:

Cpp Example