I have no problem of putting a break statement in the default option of a switch case statement, just wondering why it is needed. Anyone any ideas?

int a = 42;
            switch (a)
            {
                case 1 : 
                case 2 : //statements for case 1 and 2
                    break;
                    // probably more case here
                default:
                    Console.WriteLine("We are here");
                    break;
            }

If I leave the break out on line 10, I get the error Control cannot fall through from one case label ('default:') to another but as it is the last statement in this situation, I'm a bit puzzled.

Recommended Answers

All 6 Replies

When a switch statement contains more than one switch section, you must explicitly terminate each section, including the last one, by using one of the following keywords:

return

goto

break

throw

continue

http://msdn.microsoft.com/en-us/library/vstudio/843c4c28.aspx

Thanks for your answer IIM, but my code compiles fine and works. It would execute statements if a is either 1 or 2. It will use the default if the variable a is 42 for example. But why the break in the default is needed? Fall through can be handy in some situations.

I agree that fall-through can be handy, and I do occasionally lament its absence at my day job. But Microsoft seems to have been interested in lowering barriers to entry for a long time now, and AFAICT, this was just a decision the language designers made to prevent foot-shooting. My guess is that they required the last block to have a break as well just for consistency--they already assume we can't handle fall-through; why confuse us further? Eh, nichevo. =P

Execution of the statement list in the selected section begins with the first statement and proceeds through the statement list, typically until a jump statement is reached, such as a break, goto case, return, or throw. At that point, control is transferred outside the switch statement or to another case label.

Unlike C++, C# does not allow execution to continue from one switch section to the next. The following code causes an error.

The requirement in C# is that the end of every switch section, including the final one, is unreachable. Although this requirement usually is met by using a jump statement, the following case also is valid, because the end of the statement list cannot be reached.

C#

    case 4:
         while (true)
             Console.WriteLine("Endless looping. . . .");

Read these two paragraphs taken from http://msdn.microsoft.com/en-us/library/vstudio/06tc147t.aspx

But why the break in the default is needed?

default isn't special from a syntax standpoint, it's just another case. The reason a break is required is the same reason it's required in other cases, due to the fact that default isn't required to be the last case in the switch statement. A less common but equally valid positioning is the first case:

switch (a)
{
    default:
        Console.WriteLine("We are here");
        break;
    case 1:
    case 2:
        break;
}

And there's no requirement that default not be somewhere in the middle, though it's more confusing to read, of course. The designers of C# could have added an exception such that the last case in a switch statement doesn't require a break, but that would introduce more complexity to the language, the compiler, and force programmers to learn yet another nuance. I'd guess this was the reason it wasn't added.

Thanks all you people for the clarifications! As I said in my first post I have no problem with putting in a break or return or whatever. I was just wondering . . .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.