c++ case statement code example

Example 1: c++ switch case statement

switch (n)
{
    case 1: // code to be executed if n = 1;
        break;
    case 2: // code to be executed if n = 2;
        break;
    default: // code to be executed if n doesn't match any cases
}

Example 2: switch statement c++

switch(expression) {
   case constant-expression  :
      statement(s);
      break; //optional
   case constant-expression  :
      statement(s);
      break; //optional
  
   // you can have any number of case statements.
   default : //Optional
      statement(s);
}

Example 3: 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 4: switch case sinax c++

switch (<espressione>)
{
case <valore costante 1>:
// istruzioni
break;


case <valore costante 2>:
// istruzioni
break;
...
case <valore costante N>:
// istruzioni
break;
default:
// istruzioni
break;
}

Example 5: switch c++

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

Example 6: switch case c++

// Transfers control to one of the several statements, depending on the 
//value of a condition.
switch (variable or an integer expression) {
     case constant: {
     	//C++ code
    	 break;
     }
     case constant: {
     	//C++ code
     	break;
     }
     default: {
     	//C++ code
     	break;
   	 }
}

Tags:

Cpp Example