Understand ways to end case blocks in C#

There are several ways that you can exit case blocks in a switch statement. For example, consider the following code.

string result = "";
int control = 1;
switch (control)
    case 1:
    case 2:
        result = "One or two";
    case 3:
        result = "Three";
        for (; ; ) ;
    case 4:
        result = "Four";
        while (true) ;
    case 5:
        result = "Five";
    case 6:
        result = "Six";
        throw new ArgumentException();
    case 7:     // This one isn't allowed.
        result = "Seven";
        while (control == 7)


A switch statement contains a series of case blocks. You can place multiple case statements together in a block to make all of the cases execute the same code, but you cannot allow one block of code to fall through to the next block.

For example, in the code above cases 1 and 2 both execute the same block of code.

Most developers expect to see a break statement at the end of each case block, but there are other ways that you can end case blocks.

Usually, each block of code ends in a break statement to make execution jump out of the switch statement, but any code that prevents execution from dropping into the next block will work. As the code above demonstrates, a block can also end in an infinite for loop, an infinite while loop, a return statement, or a throw statement because all of those prevent execution from falling into the next case block.

The final case statement doesn’t work because the compiler isn’t smart enough to realize that the code doesn’t change the value of the variable control so that loop can never end.

In fact, that block doesn’t work even if you change that statement to while (true || (control == 7)) even though you can easily see that the loop can never end.

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in syntax and tagged , , , , , , , , , , . Bookmark the permalink.

2 Responses to Understand ways to end case blocks in C#

  1. Dude says:

    I’ve used return and throw statements. But can you give a practical example of when you’d end in an infinite loop?

  2. Rod Stephens says:

    Good reasons for using infinite loops are a bit unusual. This post is more to explain what’s possible not to suggest these are all good ideas.

    But for example, suppose you need the user to enter an integer in a range (or some other piece of data that must be validated). You can enter a loop sort of like this:

     for (;;)
    if (InputIsValid()) return;

    Basically the loop continues until the user gives you a valid input. Then it exits the method. (Visual Studio is smart enough to know that a break statement will exit the infinite loop and drop you into the next case statement, so you can’t use a break statement here.)

    This approach works any time you want to stay in a loop until the user enters or selects a valid condition. For example, the user might need to select a file with a certain format. (In these cases, you can rewrite the code to use a while loop instead of an infinite loop if you like.)

    For another example, suppose you’re writing a system service that monitors a directory and processes any files that appear in it. Perhaps it takes image files, resizes them, and moves them into a different directory. (I had a client who needed to do that with several directories. Basically it’s a kind of spooler.) When it starts, the program could use a switch statement to check some condition (perhaps a command line parameter) to decide which directory it should monitor. It would then enter an infinite loop to process the appropriate directory. In this example, you many not need the loop to ever exit.

Leave a Reply

Your email address will not be published. Required fields are marked *