Unconditional Branching Statements

The simplest example of an unconditional branch is a method call. You’ve caused a method branch with the WriteLine( ) methods you’ve used so far, but any method, whether a built-in part of the language or a method you create yourself (as you’ll see in Chapter 8), causes program execution to branch. When a method call is reached, the program doesn’t test to evaluate the state of any variable; the program execution branches immediately (and unconditionally) to the start of the new method.

You call a method by writing its name; for example:

UpdateSalary( ); // invokes the method UpdateSalary

As we explained, when the compiler encounters a method call, it stops execution of the current method and branches to the new method. When that new method completes its execution, the compiler picks up where it left off in the original method. You can see how this works in Figure 5-1.

Branching allows the execution to move around to different parts of your program, instead of simply proceeding in a straight line.

Figure 5-1. Branching allows the execution to move around to different parts of your program, instead of simply proceeding in a straight line.

As Figure 5-1 shows, called methods can call other methods in turn, and you’ll often see unconditional branching several methods deep. In Figure 5-1, execution begins in a method called Main( ). Statement1 and Statement2 execute; then the compiler sees a call to Method1( ). The program execution branches unconditionally to the first line of Method1( ), where the first three statements are executed. At the call to Method1A( ), the execution branches again, this time to the start of Method1A( ).

The four statements in Method1A( ) are executed, and Method1A( ) returns. Execution resumes on the first statement after the method call in Method1( ), which is Statement4. Execution continues until Method1( ) ends, at which time execution resumes back in Main( ) at Statement3. At the call to Method2( ), execution branches again; all the statements in Method2( ) execute, and then Main( ) resumes at Statement4. When Main( ) ends, the program itself ends. As you can see, branching takes straight-line program execution and breaks it up quite a bit—and this is just a simple example. However, the program always retains a strict order of execution; nothing is skipped.

You can see the effect of method calls in Example 5-1. Execution begins in Main( ), but branches to a method named SomeMethod( ). The WriteLine( ) statements in each method assist you in seeing where you are in the code as the program executes.

Example 5-1. Executing a method is the most common form of unconditional branching; when a method is called, execution jumps to the method code; when the method is complete, execution picks up where it left off in the calling method

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Example_5_1_ _ _ _Branching_to_a_Method
{
    class Program
    {
        static void Main( )
        {
            Console.WriteLine("In Main! Calling SomeMethod( )...");
            SomeMethod( );
            Console.WriteLine("Back in Main( ).");
        }
        static void SomeMethod( )
        {
            Console.WriteLine("Greetings from SomeMethod!");
        }
    }
}

The output looks like this:

In Main! Calling SomeMethod( )...
Greetings from SomeMethod!
Back in Main( ).

Each method in this example outputs a handy message to indicate where the execution is. The program flow begins in Main( ) and proceeds until SomeMethod( ) is invoked. (Invoking a method is often referred to as calling the method.) At that point, program flow branches to the method. When the method completes, program flow resumes at the next line after the call to that method.

Tip

You can also create an unconditional branch by using one of the unconditional branch keywords: goto, break, continue, return, or throw. The first three of these are discussed later in this chapter, the return statement is discussed in Chapter 7, and the final statement, throw, is discussed in Chapter 16.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset