If you thought that the if statement was sexy and cool, then, boy, do we have a treat in store for you! If you didn't, well fair enough, but here's the switch statement anyway.
switch is the if statement de-luxe. You can test a variable to see if it has any one of several values. You want to test an integer to see if it is 1, or 2, or 3, or 1000? No problem! This is how you do it:
switch ( <switch variable> )
{ case <constant expression> : <statement>;
break;
case <constant expression> : <statement>;
break;
.
.
default : <statement>;
}
What this means, when translated into English, is ... You follow the word switch with a variable in brackets. Then you put in one or more case clauses. Each word case is followed by a number or an expression which comes out as a constant number, then a colon, and then one or more statements. I'll come to the "break" and "default" bits in a little while.
Perhaps an example with numbers in it might help. Suppose we have a variable called score (Well, I'm getting sick and tired of x). q is a score out of 4 in a test. We want to print a message depending on what score is:
switch (score)
{ case 0 : cout << "That's a lousy score." << endl;
break;
case 1 : cout << "Miserable!" << endl;
break;
case 2 : cout << "You could do better, I'm sure!"
<< endl;
break;
case 3 : { cout << "Almost there!" << endl;
cout << "Try harder next time!" << endl;
}
break;
case 4 : { cout << "Well done. A perfect score."
<< endl;
cout << "Give yourself a pat on the back."
<< endl;
cout << "You can feel rather smug." << endl;
}
break;
}
This should be a lot easier to follow. There is no default clause at the end. The switch statement has been stripped down to its basics. The variable score is compared to 0, then to 1 etc. and when it matches one of the values given, the appropriate message is printed. The last two options have several statements, and these are bracketed together to form blocks.
The diagram on the left shows the basic structure of the switch statement.
The reason that the statement break; is included at the end of each case is to tell C++ where each case finishes and the next one starts. You would think that C++ would be able to work it out for itself, but apparently not. To see what I mean, try missing out the break; lines and see what happens.
If you miss out the break statements, you get this effect. Now all the statement blocks run into each other, so whichever case triggered (if any) would cause all the statements from that point on to run.
Before you start getting all excited, I should point out that switch is a lot more restricted than you think. You can only test one variable at a time, and you can only test it against constant numbers (or expressions such as 2*3+7 which evaluate to a constant number) rather than variables. However, there is nothing to stop you including if statements (or loops or even other switch statements for that matter) inside the blocks of statements that come after the case part.
switch (number_of_aliens)
{ case 0 : { win = 1; /* Indicates player wins */
if (num_ships > 1)
cout << "You have enough ships to" <<
" move to the next level." << endl;
else
cout << "Try to improve your score.";
}
break;
case 1 : for (i = 0; i < 5; i++)
alien_x_position += 2;
break;
case 2 : { cout << "The aliens have killed you!"
<< endl;
win = 2; /* Indicates aliens win */
}
break;
default : { cin >> my_try;
alien_x_position++;
}
}
Here the blocks of statements are fairly complicated. The program indicates that the user has won if there are no aliens left. If there is one alien, then its x co-ordinate is increased by 2 five times. If there are two aliens, then the user is told that he or she is "brown bread" (as we Cockneys say).
There is also a default clause. This is activated if the variable doesn't trigger any of the other clauses. It is guaranteed to match any variable if it isn't picked up first. The default clause must always be the last clause in the switch statement as it matches anything, so there would be no point putting any case statements after it. There is no break clause for this default part, as there are no later parts of the switch statement to skip over.