Well, Switch works more or less like if-else just that only equality can be checked in case of Switch and no comparisons, its a little more clutter free too.
let's say you have 3 choices to be made and different things to be done for them, here's how it works
int i;
cin>>"enter the choice ">>i;
switch(i);
{
case 1:
//statements
break;
case 2:
//statements
break;
case 3:
//statements;
break;
default:
cout<<"please enter 1 or 2 or 3";
}
So, default works when choice entered is none of the options, and the variable can also be a char.
Note that whatever the user enters its compared only and corresponding code's executed.
2007-10-31 00:10:29
·
answer #1
·
answered by ? 2
·
1⤊
0⤋
A switch can be used to perform different actions based on the value of a scalar (int, char...) variable.
For example, the following code
function demo(int some_var)
{
   if(some_var == 1)
   {
      cout << "The value is 1!";
   }
   else if(some_var == 2)
   {
      cout << "The value is 2!";
   }
   else
   {
      cout << "The value is some else... (not 1 or 2)."
   }
}
can be rewritten using switch as
function demo(int some_var)
{
   switch(some_var)
   {
      case 1:
      cout << "The value is 1!";
      break;
      case 2:
      cout << "The value is 2!";
      break;
      default:
      cout << "The value is some else... (not 1 or 2)."
      break;
   }
// Resume here...
}
Note that the 'default' is executed if none of the values in the 'case'-statements of the switch applied.
The 'break'-statements at the end of each 'case' make sure that execution of your code is resumed after the switch statement --marked with
// Resume here...
in the code above. If you forget a 'break'-statement then execution will simply continue inside the switch; for example:
function demo(int some_var)
{
   switch(some_var)
   {
      case 1:
      cout << "The value is 1!";
      // NO BREAK HERE!!!
      case 2:
      cout << "The value is 2!";
      break;
      default:
      cout << "The value is some else... (not 1 or 2)."
      break;
   }
// Resume here...
}
In this example, when the value of some_var equals 1, then
The value is 1!
The value is 2!
will be printed, because there is no 'break' between case 1 and case 2.
2007-10-30 23:30:34
·
answer #2
·
answered by Uninformed hence not consenting 7
·
1⤊
0⤋
The switch statement is a construct that is used when many conditions are being tested for. When there are many conditions, it becomes too difficult and complicated to use the if and else if constructs. Nested if/else statements arise when there are multiple alternative paths of execution based on some condition that is being tested for. Here's an example, this is a simple calculator that can be used to add, multiply, subtract, and divide. If this program was using if else statements than this program will work fine, but the if/else block is cumbersome. It would be easy, particularly if there were more choices and maybe sub choices involving more if/else's to end up with program that doesn't perform the actions intended. Here's the same program with a switch.
#include
int main(void)
{
float numb1 = 0, numb2 = 0; /* the two numbers to work on */
int menu = 1; /* add or substract or divide or multiply */
float total = 0; /* the result of the calculation */
char calType; /* what type of calculation */
printf("Please enter in the first of the two numbers\n\t");
scanf("%f", &numb1); /* READ first number */
printf("\n\nPlease enter the second of the two numbers\n\t");
scanf("%f", &numb2); /* READ second number */
printf("\n\nWhat would you like to do?\n\n"); /* WRITE instructions */
printf("\t1 = add\n");
printf("\t2 = substract\n");
printf("\t3 = multiply\n");
printf("\t4 = divide\n");
printf("\n\nPleas make your selection now:\n\t");
scanf("%d",&menu); /* READ calculation type */
switch (menu) /* select the type of calculation */
{
case 1: total = numb1 + numb2;
calType = '+'; /* assign a char to symbolise calculation type */
break;
case 2: total = numb1 - numb2;
calType = '-';
break;
case 3: total = numb1 * numb2;
calType = '*';
break;
case 4: total = numb1 / numb2;
calType = '/';
break;
default: printf("Invalid option selected\n");
}
if (menu == 3 && numb2 == 0) /* cannot divide by 0 */
printf("\n\n\tYou cannot divide by 0\n\n");
/* display result to 2 decimal places */
printf("\n\n*************************");
printf("\n\n\t%.3f %c %.3f = %.2f", numb1, calType, numb2, total);
printf("\n\n*************************\n\n");
return 0;
}
The keyword default is executed when none of the conditions being tested for in the switch statement are met or executed. The break statement must be used after each condition because if it were not used than all the conditions from the one met will be executed. For example if case 2 was met, and there was no break statement at the end of the case, case 3 and case 4 and even default would all be executed.
The general form of a switch statement is:
switch (variable) {
case expression1:
do something 1;
break;
case expression2:
do something 2;
break;
....
default:
do default processing;
}
2007-10-30 22:33:36
·
answer #3
·
answered by SPARTAN 5
·
0⤊
0⤋
int i;
cin>>"enter the choice ">>i;
switch(i);
case 1:
cout<<"you have chosen tomato";
return;
case 2:
cout<<"you have chosen potato";
return;
default:
cout<<"please enter 1 or 2";
2007-10-30 22:28:27
·
answer #4
·
answered by binay 3
·
0⤊
1⤋
switch need a parameter
when user insert the parameter go and check if it accept or Not
switch (X)
{
case(1);
do some thing;
.
.
.
}
2007-10-30 22:28:54
·
answer #5
·
answered by MohD 2
·
0⤊
1⤋