Control Structures

Normally, statements in an application are executed one after the other in the order in which they are written. This process is called sequential execution. Various C# statements, which we will soon discuss, enable you to specify that the next statement to execute is not necessarily the next one in sequence. This is called transfer of control.

During the 1960s, it became clear that the indiscriminate use of transfers of control was the root of much difficulty experienced by software development groups. The blame was pointed at the goto statement (used in most programming languages of the time), which allows programmers to specify a transfer of control to one of a very wide range of possible destinations in an application (creating what is often called "spaghetti code"). The notion of so-called structured programming became almost synonymous with "goto elimination." We recommend that you avoid C#'s goto statement.

The research of Bohm and Jacopini[1] had demonstrated that applications could be written without goto statements. The challenge of the era for programmers was to shift their styles to "goto-less programming." Not until the 1970s did programmers start taking structured programming seriously. The results were impressive. Software development groups reported shorter development times, more frequent on-time delivery of systems and more frequent within-budget completion of software projects. The key to these successes was that structured applications were clearer, easier to debug and modify, and more likely to be bug free in the first place.

[1] Bohm, C., and G. Jacopini, "Flow Diagrams, Turing Machines, and Languages with Only Two Formation Rules," Communications of the ACM, Vol. 9, No. 5, May 1966, pp. 336371.

Bohm and Jacopini's work demonstrated that all applications could be written in terms of only three control structuresthe sequence structure, the selection structure and the repetition structure. The term "control structures" comes from the field of computer science. When we introduce C#'s implementations of control structures, we will refer to them in the terminology of the C# Language Specification as "control statements."

Sequence Structure in C#

The sequence structure is built into C#. Unless directed otherwise, the computer executes C# statements one after the other in the order in which they are writtenthat is, in sequence. The UML activity diagram in Fig. 5.1 illustrates a typical sequence structure in which two calculations are performed in order. C# lets you have as many actions as you want in a sequence structure. As you will soon see, anywhere a single action may be placed, you may place several actions in sequence.

Figure 5.1. Sequence structure activity diagram.

An activity diagram models the workflow (also called the activity) of a portion of a software system. Such workflows may include a portion of an algorithm, such as the sequence structure in Fig. 5.1. Activity diagrams are composed of special-purpose symbols, such as action-state symbols (rectangles with their left and right sides replaced with arcs curving outward), diamonds and small circles. These symbols are connected by transition arrows, which represent the flow of the activitythat is, the order in which the actions should occur.

Like pseudocode, activity diagrams help you develop and represent algorithms, although many programmers prefer pseudocode. Activity diagrams clearly show how control structures operate.

Consider the activity diagram for the sequence structure in Fig. 5.1. It contains two action states that represent actions to perform. Each action state contains an action expressionfor example, "add grade to total" or "add 1 to counter"that specifies a particular action to perform. Other actions might include calculations or input/output operations. The arrows in the activity diagram represent transitions, which indicate the order in which the actions represented by the action states occur. The portion of the application that implements the activities illustrated by the diagram in Fig. 5.1 first adds grade to total, then adds 1 to counter.

The solid circle located at the top of the activity diagram represents the activity's initial statethe beginning of the workflow before the application performs the modeled actions. The solid circle surrounded by a hollow circle that appears at the bottom of the diagram represents the final statethe end of the workflow after the application performs its actions.

Figure 5.1 also includes rectangles with the upper-right corners folded over. These are UML notes (like comments in C#)explanatory remarks that describe the purpose of symbols in the diagram. Figure 5.1 uses UML notes to show the C# code associated with each action state in the activity diagram. A dotted line connects each note with the element that the note describes. Activity diagrams normally do not show the C# code that implements the activity. We use notes for this purpose here to illustrate how the diagram relates to C# code. For more information on the UML, see our optional case study, which appears in the Software Engineering Case Study sections at the ends of Chapters 1, 39 and 11, and visit www.uml.org.

Selection Structures in C#

C# has three types of selection structures, which from this point forward, we shall refer to as selection statements. These are discussed in this chapter and Chapter 6. The if statement either performs (selects) an action if a condition is true or skips the action if the condition is false. The if...else statement performs an action if a condition is true or performs a different action if the condition is false. The switch statement (Chapter 6) performs one of many different actions, depending on the value of an expression.

The if statement is called a single-selection statement because it selects or ignores a single action (or, as we will soon see, a single group of actions). The if...else statement is called a double-selection statement because it selects between two different actions (or groups of actions). The switch statement is called a multiple-selection statement because it selects among many different actions (or groups of actions).

Repetition Structures in C#

C# provides four repetition structures, which from this point forward, we shall refer to as repetition statements (also called iteration statements or loops). Repetition statements enable applications to perform statements repeatedly, depending on the value of a loop-continuation condition. The repetition statements are the while, do...while, for and foreach statements. (Chapter 6 presents the do...while and for statements. Chapter 8 discusses the foreach statement.) The while, for and foreach statements perform the action (or group of actions) in their bodies zero or more timesif the loop-continuation condition is initially false, the action (or group of actions) will not execute. The do...while statement performs the action (or group of actions) in its body one or more times.

The words if, else, switch, while, do, for and foreach are C# keywords. Recall that keywords are used to implement various C# features, such as control statements. Keywords cannot be used as identifiers, such as variable names. A complete list of C# keywords appears in Fig. 3.2.

Summary of Control Statements in C#

C# has only three kinds of structured control statements: the sequence statement, selection statement (three types) and repetition statement (four types). Every application is formed by combining as many sequence, selection and repetition statements as is appropriate for the algorithm the application implements. As with the sequence statement in Fig. 5.1, we can model each control statement as an activity diagram. Each diagram contains an initial state and a final state that represent a control statement's entry point and exit point, respectively. Single-entry/single-exit control statements make it easy to build applicationsthe control statements are "attached" to one another by connecting the exit point of one to the entry point of the next. This procedure is similar to the way in which a child stacks building blocks, so we call it control-statement stacking. You will learn that there is only one other way in which control statements may be connected: control-statement nesting, in which a control statement appears inside another control statement. Thus, algorithms in C# applications are constructed from only three kinds of structured control statements, combined in only two ways. This is the essence of simplicity.

if Single Selection Statement

Категории