# Help understanding Switch Statement: Case within another case

#### integral

Joined Aug 19, 2010
22
Hello ,

I need help understanding what is going on in the program below. I got this program from example 2 towards the bottom of the page from the following c++ tutorial: http://xoax.net/comp/cpp/console/Lesson27.php

If someone can explain in words exactly what is happening I will appreciate it. I understand switch statements however this is the first time that I have come across a switch statement that contains a case within another case. I dont understand why I end up with the results I get for the case that contains another case. For example, when the line in green is executed what happens that "Mystery" is not outputted but Novel and Fiction get outputted?

Here is the code:
Rich (BB code):
#include <iostream>

enum EBookType { keMystery,
keNovel,
keFiction,
keNonFiction};

void PrintBookType( EBookType eType)
{
using namespace std;
switch (eType) {
case keMystery:
{
cout << "Mystery" << endl;
case keNovel:
{
cout << "Novel" << endl;
case keFiction:
{
cout << "Fiction" << endl;
break;
}
}
}

case keNonFiction:
{
cout << "Non-fiction" << endl;
break;
}
default:
{
cout << "unknown type" << endl;
break;
}
}
}

int main()
{
using namespace std;
EBookType eAtlasShrugged = keMystery;
EBookType eLionWitchWardrobe = keNovel;
EBookType eTheRepublic = keNonFiction;

PrintBookType(eAtlasShrugged);
cout << endl;
PrintBookType(eLionWitchWardrobe);
cout << endl;
PrintBookType(eTheRepublic);
cout << endl;
PrintBookType( (EBookType) 85 );

return EXIT_SUCCESS;

}
Here is the program output:
Rich (BB code):
Mystery
Novel
Fiction

Novel
Fiction

Non-fiction

unknown type
Press any key to continue . . .

Joined Jul 7, 2009
1,583
I could tell you a straight answer, but you won't learn much. Instead, I'll suggest two things you can do to learn more. First, step through the curious code with a debugger and examine the values of all the variables and understand how they are changing. In particular, watch how the switch statement behaves when you give it the green line's value. A hint in doing debugging is to remove all the code that isn't relevant to the problem so you can focus on the key operations.

The real learning will come when you dump the assembly output of your code. This will let you see how a switch statement works. You don't need any real training in the assembly language of the platform you're on -- you should be able to puzzle things out by their names and looking things up on the web.

By the way, nice job on submitting a compilable and working chunk of code along with output. Many of the questions are tautologically equivalent to "I have a piece of rope. How long is it?"

#### ErnieM

Joined Apr 24, 2011
8,055
It may be clearer to rewrite the case statement without the brackets, as they have no effect (as you have seen):

Rich (BB code):
void PrintBookType( EBookType eType)
{
using namespace std;
switch (eType) {
case keMystery:
cout << "Mystery" << endl;

case keNovel:
cout << "Novel" << endl;

case keFiction:
cout << "Fiction" << endl;
break;

case keNonFiction:
cout << "Non-fiction" << endl;
break;

default:
cout << "unknown type" << endl;
break;
}
}
To explain the unexpected results, consider when passed eType = keNovel. The switch statement jumps down to case keNovel where it starts executing code until it sees a break statement. Hence it prints both "Novel" and "Fiction".

Note I disagree with viewing the assembly dump. Avoiding messing with assembly is the main reason for going to a high level language, though reading it is not as error prone as writing it. However, single stepping thru any code should be your first step after getting it to compile.

Joined Jul 7, 2009
1,583
Note I disagree with viewing the assembly dump.
Where I used to work, we'd have the newbie firmware engineers do this because it would teach them a little about how compilers work and how to think like a compiler (of course, if they were CS-types, they should have taken a class on compiler construction and have known this). It's just another tool in the toolbox -- and it will become useful someday.

#### ErnieM

Joined Apr 24, 2011
8,055
Sure it's a good tool, but currently I'm doing PIC32's and I'm completely clueless as to what that assembly could possibly mean.

The past few days I was tracking down a linker problem leading to a malloc() call fault where malloc would never return. My lib for malloc doesn't include the C source so no C tracing is possible. I discovered the problem by single stepping thru program memory looking at God knows what as statements, but did notice the statements were all in a few dozen instructions of each other, hence knew it was stuck in a loop.

I don't know PIC32 assembly and I have no intentions of ever learning it.

#### integral

Joined Aug 19, 2010
22
It may be clearer to rewrite the case statement without the brackets, as they have no effect (as you have seen):

To explain the unexpected results, consider when passed eType = keNovel. The switch statement jumps down to case keNovel where it starts executing code until it sees a break statement. Hence it prints both "Novel" and "Fiction".
Thanks for this. Rewriting it withouth the braces really cleared things up. The braces confused me - I was comparing each "embedded" case statement to "embedded" if statements.