5.4 Control Structures

Normally, statements execute one after the other in the order in which they’re written. This process is called sequential execution. Various C# statements 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 you to specify a transfer of control to one of a wide range of destinations in a program.

The research of Bohm and Jacopini1 had demonstrated that programs could be written without any goto statements. The challenge for programmers of the era was to shift their styles to “goto-less programming.” The term structured programming became almost synonymous with “goto elimination.” Not until the 1970s did most 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 programs were clearer, easier to debug and modify, and more likely to be bug free in the first place.

Bohm and Jacopini’s work demonstrated that all programs could be written in terms of only three control structures—the sequence structure, the selection structure and the iteration structure. We’ll discuss how each of these is implemented in C#.

5.4.1 Sequence Structure

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’re written—that is, in sequence. The UML activity diagram in Fig. 5.2 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.

Fig. 5.2 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, like the sequence structure in Fig. 5.2. Activity diagrams are composed of symbols, such as action-state symbols (rectangles with their left and right sides replaced with outward arcs), diamonds and small circles. These symbols are connected by transition arrows, which represent the flow of the activity—that is, the order in which the actions should occur.

Like pseudocode, activity diagrams help you develop and represent algorithms. Activity diagrams clearly show how control structures operate. We use the UML in this chapter and the next to show the flow of control in control statements.

Consider the sequence-structure activity diagram in Fig. 5.2. It contains two action states, each containing an action expression—for example, “add grade to total” or “add 1 to counter”—that specifies a particular action to perform. 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 app that implements the activities illustrated in Fig. 5.2 first adds grade to total, then adds 1 to counter.

The solid circle at the top of the activity diagram represents the initial state—the beginning of the workflow before the app performs the modeled actions. The solid circle surrounded by a hollow circle at the bottom of the diagram represents the final state—the end of the workflow after the app performs its actions.

Figure 5.2 also includes rectangles with the upper-right corners folded over. These are UML notes (like comments in C#) that describe the purpose of symbols in the diagram. Figure 5.2 uses UML notes to show the C# code associated with each action state. 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 do this here to illustrate how the diagram relates to C# code.

5.4.2 Selection Statements

C# has three types of selection structures, which from this point forward we shall refer to as selection statements. The if statement performs (selects) an action if a condition is true or skips the action if the condition is false. The ifelse 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’ll soon see, a single group of actions). The ifelse 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).

5.4.3 Iteration Statements

C# provides four iteration statements (sometimes called repetition statements or looping statements) that enable programs to perform statements repeatedly as long as a condition (called the loop-continuation condition) remains true. The iteration statements are the while, dowhile, for and foreach statements. (Chapter 6 presents the dowhile 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 times—if the loop-continuation condition is initially false, the action (or group of actions) will not execute. The dowhile 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.

5.4.4 Summary of Control Statements

C# has only three kinds of control statements: the sequence, selection (three types) and iteration (four types). Every app is formed by combining as many of these statements as is appropriate for the algorithm the app implements. We can model each control statement as an activity diagram. Like Fig. 5.2, 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 programs—we simply connect the exit point of one to the entry point of the next. We call this control-statement stacking. We’ll learn that there’s only one other way in which control statements may be connected—control-statement nesting—in which one control statement appears inside another. Thus, algorithms in C# apps are constructed from only three kinds of control statements, combined in only two ways. This is the essence of simplicity.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset