Unconditional Branching Statements






Unconditional Branching Statements

The most simple example of an unconditional branch is a method call. When a method call is reached, there is no test made to evaluate the state of the object; 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 I explained earlier in the chapter, 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. This process is illustrated schematically in Figure.

How branching works


As Figure suggests, it is actually quite common for there to be unconditional branching several methods deep. In Figure, execution begins in a method called Main( ). Statement1 and Statement2 execute; then the compiler sees a call to Method1( ). Program execution branches unconditionally to the first line of Method1( ), where its first three statements are executed. At the call to Method1A( ), execution again branches, 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( ) (Statement4). Execution continues until Method1( ) ends, at which time execution resumes back in Main( ) at Statement3. At the call to Method2( ), execution again branches; all the statements in Method2( ) execute, and then Main( ) resumes at Statement4. When Main( ) ends, the program itself ends.

You can see the effect of method calls in Figure. 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.

Branching to a method

using System;
class Functions
{
   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(  ).

Program flow begins in Main( ) and proceeds until SomeMethod( ) is invoked. (Invoking a method is sometimes 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.

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


Methods and their parameters and return values are discussed in detail in Chapter 8.



 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows