Answers for "switch statement in c++"

C++
155

c++ switch

switch(expression) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
}
Posted by: Guest on February-21-2020
33

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
}
Posted by: Guest on March-12-2020
12

switch c++

switch(expression) {
   case 1:
      //equivalent to if(expression == 1){//do someting...}
      //do something...
      break; 
    //if case 1 is true the rest of the statments arn't 
    //evaluated because of the break
   case 45:
      //equivalent to else if(expression == 45){//do someting...}
      //do something...
      break;
    
   // you can have any number of case statements and default has to be last
   default :
      // equivalent to else{//do someting...}
      //do something...
}

switch(expression) {
   case 1:
      //equivalent to if(expression == 1){//do someting...}
      //do something...
   case 45:
      //equivalent to if(expression == 45){//do someting...}
      //do something...
   default :
      //always runs if there are no breaks in any of the cases
      //do something...
}

//modification of answer by Homeless Hoopoe
Posted by: Guest on May-25-2020
12

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);
}
Posted by: Guest on March-23-2020
2

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;
}
Posted by: Guest on March-04-2020
2

switch statement in c++

#include <iostream>
using namespace std;
 
int main () {
   // local variable declaration:
   char grade = 'D';

   switch(grade) {
      case 'A' :
         cout << "Excellent!" << endl; 
         break;
      case 'B' :
      case 'C' :
         cout << "Well done" << endl;
         break;
      case 'D' :
         cout << "You passed" << endl;
         break;
      case 'F' :
         cout << "Better try again" << endl;
         break;
      default :
         cout << "Invalid grade" << endl;
   }
   cout << "Your grade is " << grade << endl;
 
   return 0;
}
Posted by: Guest on November-09-2020

Browse Popular Code Answers by Language