4.4.Branching Statements

Branching statement Description
break Leaves the switch block
continue Leaves the switch block, skips remaining logic in enclosing loop, and goes back to loop condition to determine if loop should be executed again from the beginning. Works only if switch statement is in a loop as described in Lesson 04: Control Statements – Loops.

goto Leaves the switch block and jumps directly to a label of the form “:”
return Leaves the current method. Methods are described in more detail in Lesson 05: Methods.

throw Throws an exception, as discussed in Lesson 15: Introduction to Exception Handling.

You may also include a default choice following all other choices. If none of the other choices match, then the default choice is taken and its statements are executed. Although use of the default label is optional, I highly recommend that you always include it. This will help catch unforeseen circumstances and make your programs more reliable.
Each case label must end with a branching statement, as described in table 3-1, which is normally the break statement. The break statement will cause the program to exit the switch statement and begin execution with the next statement after the switch block. There are two exceptions to this: adjacent case statements with no code in between or using a goto statement. Here’s an example that shows how to combine case statements:

        switch (myInt)
        {
            case 1:
            case 2:
            case 3:
                Console.WriteLine("Your number is {0}.", myInt);
                break;
            default:
                Console.WriteLine("Your number {0} is not between 1 and 3.", myInt);
                break;
        }

By placing case statements together, with no code in-between, you create a single case for multiple values. A case without any code will automatically fall through to the next case. The example above shows how the three cases for myInt equal to 1, 2, or 3, where case 1 and case 2 will fall through and execute code for case 3.
A case statement can only be an exact match and you can’t use logical conditions. If you need to use logical conditions, you can use an if/else if/else statement.
Another way to control the flow of logic in a switch statement is by using the goto statement. You can either jump to another case statement, or jump out of the switch statement. The second switch statement in Listing 3-2 shows the use of the goto statement, as shown below:

 
        // switch with string type
        switch (myInput)
        {
            case "continue":
                goto begin;
            case "quit":
                Console.WriteLine("Bye.");
                break;
            default:
                Console.WriteLine("Your input {0} is incorrect.", myInput);
                goto decide;
        }

Note: in the current example, “continue”, is a case of the switch statement — not the keyword.
The goto statement causes program execution to jump to the label following the goto keyword. During execution, if the user types in “continue”, the switch statement matches this input (a string type) with the case “continue”: label and executes the “goto begin:” instruction. The program will then leave the switch statement and start executing the first program statement following the begin: label. This is effectively a loop, allowing you to execute the same code multiple times. The loop will end when the user types the string “quit”. This will be evaluated with the case “quit”: choice, which will print “Bye.” to the console, break out of the switch statement and end the program.
Warning: You should not create loops like this. It is *bad* programming style. The only reason it is here is because I wanted to show you the syntax of the goto statement. Instead, use one of the structured looping statements, described in Lesson 04: Control Statements – Loops.
When neither the “continue” nor “quit” strings are entered, the “default:”case will be entered. It will print an error message to the console and then execute the goto decide: command. This will cause program execution to jump to the first statement following the decide: label, which will ask the user if they want to continue or quit. This is effectively another loop.
Clearly, the goto statement is powerful and can, under controlled circumstances, be useful. However, I must caution you strongly on its use. The goto statement has great potential for misuse. You could possibly create a very difficult program to debug and maintain. Imagine the spaghetti code that could be created by random goto statements throughout a program. In the next lesson, I’ll show you a better way to create loops in your program.
Summary
The if statement can be written in multiple ways to implement different branches of logic. The switch statement allows a choice among a set of bool, enum, integral, or string types. You use break, continue, goto, return, or throw statements to leave a case statement. Be sure to avoid the goto statement in your code unless you have an extremely good reason for using it.
In addition to branching based on a condition, it is useful to be able to execute a block of statements multiple times. A goto statement is not proper or adequate for such logic. Therefore, I invite you to return for Lesson 4: Control Statements – Loops. This will be a continuation of the same topic.

Yes No Suggest edit
Suggest Edit