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( ...

Get Learning C# 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.