Answers for "switch case c++"

C++
5

c++ switch multiple cases

#include <iostream> 
using namespace std; 
  
int main() { 
    // variable declaration 
	int input;
	switch(input){
	case 1: case 2: case 3: case 4:
		//executes if input is 1, 2, 3, or 4
		break;
	case 5: case 6: case 7:
		//executes if input is 5, 6, or 7
        break;
	default:
		//executes if input isn't any of the cases
    }
    return 0; 
}
Posted by: Guest on September-22-2020
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
1

c++ switch

switch(a) {
  case -1:
    std::cout << "a == -1" << std::endl;
  break;

  case 0:
    std::cout << "a == 0" << std::endl;
  break;

  default:
    std::cout << "a is something else" << std::endl;
}
Posted by: Guest on September-29-2021
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
0

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;
   	 }
}
Posted by: Guest on April-30-2021

Browse Popular Code Answers by Language