Using <I>switch</I>


One of the last points of C++ we haven't covered is the switch statement. What is it? A revamp of Pascal's case statement. The works are pretty similar, but there are small differences, both in its syntax, and its workings. Let's start off by showing a switch in C++ versus a case in Pascal:

Pascal:
case Grade of
   'A' : Writeln('Yay, you got an A!');
   'B' : Writeln('Hmm, a B. Not bad.');
   'C' : Writeln('A C. Oh well.');
   'D' : Writeln('A D? Argh.');
   'F' : Writeln('This is not happening..');
end;
C++:
switch(Grade)
{
   case 'A' : cout << "Yay, I got an A!" << endl;
	      break; 
   case 'B' : cout << "Hmm, a B. Not bad." << endl;
	      break;
   case 'C' : cout << "A C. Oh well." << endl;
              break;
   case 'D' : cout << "A D? Argh." << endl;
              break;
   case 'F' : cout << "This is not happening.." << endl;
              break;
}

Note: Don't worry about the weird cout thing above -- that's just C++'s equivalent of Write. All will be revealed in Chapter 2.

The first thing you should notice is that every line has a statement under it called "break". What's it for? Well, in C++, the label case on each line is just that -- a label that tells it where to start running statements. What does this mean? Well, you can place many lines in a row. In fact, if you don't put a break in, the program will keep going down in the case statements, into other case labels, until it reaches the end of a break. Think of the break as the only thing that stops a case statement from continuing all the way down through each case label under it. For instance, look at this piece of code:

switch (borp)
{
   case 1 : cout << "One." << endl; 
   case 2 : cout << "Two." << endl;
   case 3 : cout << "Three." << endl;
   default: cout << "Huh?" << endl;
}

Suppose that borp was given the value 2. Because there are no break's to stop the execution, the program would keep going, giving the output:

Two.
Three.
Huh?

Of course, this is probably not what you wanted. Simply adding a break to each case statement will clear that up. Now, you might be wondering what that default part is. You might remember that in Pascal, you could put an else at the end of a case statement, and if a value didn't match the ones you listen in the statement, it would use that one instead. Well, this is C++'s version.

One big note: In many variants of Pascal, it was possible to have one of your labels in a case statement be a string. This is not in the official Pascal standard, and it isn't in the C++ standard either. The reason? The switch statement in C++ only allows for an integer. Since a char (ie 'A' and so on above) can be converted to a number inside the program, they're allowed, but there's no way to turn "Burritos!" into a single number, so it can't be used.


Table of Contents

The Big Picture | Comments | Variables | More on Variables
Variable Operations | Making Choices | More on if | Using else
Using switch | The while Loop | The do..while Loop | Constants
The for Loop | More on the for Loop | Exercises