
 |
![]() |
|
|
|
|
 |
The switch Statement
Here is a nifty little statement, called the switch statement. Its syntax is:
 | |  | | | switch (expression)
{
case valueOne: statement; //I prefer the first one
break;
case valueTwo: statement;
break;
....
case valueN: statement;
break;
default: statement;
} |
|  |
|
or
 | |  | | | switch (expression)
{
case constant1: statementList1;
case constant2: statmentList2;
:
:
case constantN: statementListN;
default: statementList;
} |
|  |
|
The switch statement evaluates the expression and then looks for its value among the case constants. If the value if found among the constants listed, then the statements in that statementList are executed. Otherwise if there is a default (which is optional), then the program breaks to that statementList. Note that the expression must evaluate to an integer type and that the constants must be integer constants.
This may sound a little confusing but just bare with me and check out this code, it will all start to make sense (I hope).
 | |  | | | #include <iostream.h>
int main()
{
int n;
cout <<"Enter any number, but nothing past 10: ";
cin >> n;
switch(n)
{
case 10:
cout << "10 must be your favorite #" << endl;
break;
case 9:
cout << "9 must be your favorite #" << endl;
break;
case 8:
cout << "8 must be your favorite #" << endl;
break;
case 7:
cout << "7 must be your favorite #" << endl;
break;
case 6:
cout << "6 must be your favorite #" << endl;
break;
case 5:
cout << "5 must be your favorite #" << endl;
break;
case 4:
cout << "4 must be your favorite #" << endl;
break;
case 3:
cout << "3 must be your favorite #" << endl;
break;
case 2:
cout << "2 must be your favorite #" << endl;
break;
case 1:
cout << "1 must be your favorite #" << endl;
break;
case 0:
cout << "0 must be your favorite #" << endl;
break;
default:
cout << "Error: you didn't follow directions! << endl;
}
return 0;
} |
|  |
|
Yes I know, this has to be one of the most pointless programs I have written in this tutorial so far J. But I think it illustrates the point well, the user is prompted for a number 0-10. That number is given to the switch statement. If the number is 0, the case statement 0 matches up, and the message 0 must be your favorite # is printed, and the break statement ends the switch. If the value is 5, execution switches to case 5, where a message is printed. If I took out the keyword break at the end of one of the cases, it would fall through to where the next break is. For instance, say I typed in, 10 as the number of choice and didn't have any break statements at the end except after the default statement. It would drop all the way down to the end of the code and print all the messages.Please Rate this Code:
Comments for: The switch Statement
Annonymouse User says:
i found this very useful. also, instead of break, you can use a goto. I modded the script to add a trick number and if you type that it goes into an infinate loop, pointless, but good cause im only learning :D |
 |
 |
Annonymouse User says:
i found this very useful. also, instead of break, you can use a goto. I modded the script to add a trick number and if you type that it goes into an infinate loop, pointless, but good cause im only learning :D says:
thanks that helped me
|
 |
 |
MoMad [big_mo_mine@yahoo.com] Posted: 31 times. says:
Is it just me or did anyone realize the repeat what the previous post said and add "says:" glitch?? Its really annoying. |
 |
 |
MoMad [big_mo_mine@yahoo.com] Posted: 31 times. says:
Is it just me or did anyone realize the repeat what the previous post said and add "says:" glitch?? Its really annoying. says:
Very useful indeed |
 |
 |
cheesehead [someone@somewhere.com] Posted: 4 times. says:
Yes, it is a trivial problem in a trivial program, but this program doesn\'t deal with the case of negitive numbers, which I believe satisfy the directions but would result in the \"Error: you didn\'t follow directions!\" message. Also, there is a \'\"\' missing from the default case. |
 |
 |
|
 |
|
 |
|
|
|
|
|
|
|