Introduction to Flow of Control

   


In the Shakespeare.cs program presented in Chapter 2, "Your First C# Program," all statements were executed sequentially in the order they were written in the source code. This is called sequential execution. Normally, statements in a method are executed sequentially; that is, if none of the constructs, presented in this and the next chapter that are specifically designed to redirect this otherwise linear flow of events, are encountered.

A program based purely on sequential execution will, during each execution, always perform exactly the same actions; it is unable to react in response to current conditions.

But life is seldom that simple. Often, programs need to alter the flow of control in a program.

The flow of control is the order in which statements of a program are executed. Other terms used are ordering and control flow.

For example, the Hello.cs program of Listing 3.1 in Chapter 3, "A Guided Tour Through C#: Part 1," was able to let user input (yes or no) influence its flow of control by deciding whether the statement printing "Hello World!" should be executed. Implementing this type of logic calls for the ability to use a given condition to decide between two or more alternative branches. The if statement, informally presented in the Hello.cs program, together with the yet to be presented switch statement, form the group of statements used for this exact purpose and are collectively called branching statements.

A branch is a segment of a program made up of one statement or a group of statements.

A branching statement chooses to execute just one of several statements or blocks of statements. The choice made depends on a given condition. Branching statements are also called selection statements or alternation statements.

Note

graphics/common.gif

C# supports less obvious ways of directing the flow of control through a couple of mechanisms called virtual method invocation (or dynamic binding) and delegate invocation. Due to the advanced object-oriented nature of these constructs, their treatment has been deferred to Chapter 17, "Inheritance Part II: Abstract Functions, Polymorphism, and Interfaces," and Chapter 20, "Delegates and Events."


Frequently, programs need to repeat the same actions over and over until a given condition is met. The BliposClock.cs program in Listing 6.8 in Chapter 6, "Types, Part I: The Simple Types" utilized the do-while loop to repeatedly let the user move the simulated Blipos Clock backward and forward until a given input ("T" for terminated) was provided. Without the ability to repeatedly reuse the same statements, the length of the BliposClock.cs program would be directly proportional to the number of times the user would want to adjust the clock. If the repeated sequence contained ten lines of code and the user wanted to adjust the clock one thousand times, this part of the program would have to occupy a staggering ten thousand lines of code. Apart from wasting valuable programmer time and computer memory, this latter approach would force the user to provide exactly one thousand clock adjustments every time the program was executed.

The familiar while and do-while statements, together with the hitherto not presented for statement, fortunately solve these serious problems. They belong to a group of statements called iteration statements.

An iteration statement repeats a statement or a block of statements until a given termination condition is met. Iteration statements are also referred to as loop statements or repetition statements.

Note

graphics/common.gif

Whenever the flow of control is not directed by a branching or an iteration statement or something similar, C# executes all statements in a sequential fashion.


Note

graphics/common.gif

Recursion, like iteration, constitutes a mechanism that repeatedly performs the same set of operations. However, in contrast to iteration statements, the repetition is accomplished by letting a method call itself over and over again until some termination condition is met. This seemingly peculiar process can be valuable for solving many computational problems of a recursive nature. Recursion is purely done through method calls and not associated with any special C# keywords. Due to the advanced nature of recursion, it is not presented until Chapter 23, "Recursion Fundamentals."


Not only do the branching and loop statements allow a program to respond appropriately to a variety of different user input, they also allow us to implement the underlying rules and logic governing complex systems, such as airplane navigation systems, the neurons of the brain, or ant colonies.

In fact, any program of much use involves one or more of the constructs already mentioned.

An essential element of branching and looping is the ability to compare values. In C#, this is facilitated through the use of comparison operators. The equality (==) operator presented in the Hello.cs program of Listing 3.1 in Chapter 3, "A Guided Tour Through C#: Part I" is an example of a comparison operator. It allows the program to check whether the statement "user input is equal to 'y'" is true or false and triggers the if statement to execute the appropriate statements.

Often, several comparisons must be combined before a decision can be made. For example, you might only go for a walk if the sun is shining and you are not working. The word "and" allows us to combine two individual comparisons and make a final conclusion. In C#, "and" belongs to a group of operators termed logical operators.

This chapter focuses on the branching statements, along with the closely related comparison and logical operators. The next chapter concludes the treatment of flow of control by looking at the iteration statements.


   


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

Similar book on Amazon

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