Section 6.1. Unconditional Branching Statements

   

6.1 Unconditional Branching Statements

The simplest example of an unconditional branch is a method call. When a method call is reached, no test is 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 

It is also legal to call a VB.NET method with the optional keyword Call :

 Call Method1( ) 

However, if you do use Call on a function, the return value is discarded. Since this represents a disadvantage and there is no other advantage to this syntax, it won't be used in this book.

As explained in the introduction, 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 6-1.

Figure 6-1. How branching works
figs/lvbn_0601.gif

As Figure 6-1 suggests, it is actually quite common for there to be unconditional branching several methods deep. In Figure 6-1, 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( ) (Statement 4). 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 Example 6-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 6-1. Branching to a method
 Option Strict On Imports System Module Module1    Sub Main( )       Console.WriteLine("In Main! Calling SomeMethod( )...")       SomeMethod( )       Console.WriteLine("Back in Main( ).")    End Sub 'Main    Sub SomeMethod( )       Console.WriteLine("Greetings from SomeMethod!")    End Sub 'SomeMethod End Module 
  Output  : 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 , Exit , Return , or Throw . The first three of these are discussed later in this chapter, while the final statement, throw , is discussed in Chapter 17.

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

   


Learning Visual Basic. NET
Learning Visual Basic .Net
ISBN: 0596003862
EAN: 2147483647
Year: 2002
Pages: 153
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net