5.2. Control StructuresThe 1960s research of Bohm and Jacopini demonstrated that all programs containing GoTo statements could be written without them and that all programs could be written in terms of only three control structures[1]the sequence structure, the selection structure and the repetition structure. The term "control structures" comes from the field of computer science. When we introduce Visual Basic's implementation of control structures, we will refer to them as "control statements."
Sequence Structure in Visual BasicThe UML activity diagram in Fig. 5.1 illustrates a typical sequence structure in which two calculations are performed in order. Visual Basic lets us have as many actions as we want in a sequence structure. As we will soon see, anywhere a single action may be placed, we may place several actions in sequence. Figure 5.1. Sequence-structure activity diagram.The two statements in Fig. 5.1 involve adding a grade to a total variable and adding the value 1 to a counter variable. Such statements might appear in a program that takes the average of several student grades. To calculate an average, the total of the grades being averaged is divided by the number of grades. A counter variable would be used to keep track of the number of values being averaged. You'll see similar statements in the program in Section 5.9. UML Activity DiagramsActivity diagrams are part of the UML. An activity diagram models the workflow (also called the activity) of a portion of a software system. A workflow 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 the action state symbol (a rectangle with its left and right sides replaced with arcs curving outward; there are two of these in Fig. 5.1), the diamond symbol (there are none in this figure) and the small circle symbol;these symbols are connected by transition arrows, which represent the flow of the activity. Activity diagrams help programmers develop and represent algorithms. Activity diagrams clearly show how control structures operate. Consider the sequence-structure activity diagram in Fig. 5.1. It contains two action states that represent actions to perform. Each action state contains an action expression"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 transition arrows represent transitions that indicate the order in which the actions represented by the action states occurthe program that implements the activities illustrated by the activity diagram in Fig. 5.1 first adds grade to total, then adds 1 to counter. The solid circle symbol located at the top of the activity diagram represents the activity's initial statethe beginning of the workflow before the corresponding program performs the modeled activities. The solid circle surrounded by a hollow circle that appears at the bottom of the activity diagram represents the final statethe end of the workflow after the corresponding program performs its activities. Figure 5.1 also includes rectangles with the upper-right corners folded over. These are called notes in the UML. Notes are optional explanatory remarks that describe the purpose of symbols in the diagram. Notes can be used in any UML diagramnot just activity diagrams. Figure 5.1 uses UML notes to show the Visual Basic code associated with each action state in the activity diagram. A dotted line connects each note with the element that the note describes. An activity diagram normally does not show the Visual Basic code that implements the activity. We use notes here for this purpose to illustrate how the diagram relates to Visual Basic code. For more information on the UML, visit www.uml.org. Selection Statements in Visual BasicVisual Basic provides three types of selection statements. The If...Then selection statement (which we introduced briefly in Chapter 3 and study in detail in Section 5.3) either performs (selects) an action (or sequence of actions) if a condition is true, or skips the action (or sequence of actions) if the condition is false. The If...Then...Else selection statement, which we introduce in Section 5.4, performs an action (or sequence of actions) if a condition is true, and performs a different action (or sequence of actions) if the condition is false. The Select...Case statement, which we introduce in Chapter 6, performs one of many different actions (or sequences of actions), depending on the value of an expression. The If...Then statement is called a single-selection statement because it selects or ignores a single action (or a sequence of actions). The If...Then...Else statement is called a double-selection statementbecause it selects between two different actions (or sequences of actions). The Select...Case statement is called a multiple-selection statement because it selects among many different actions or sequences of actions. Repetition Statements in Visual BasicVisual Basic provides seven types of repetition statements (also called looping statements or loops) that enable programs to perform statements repeatedly based on the value of a condition. The repetition statements are the While, Do While...Loop, Do...Loop While, Do Until...Loop, Do...Loop Until, For...Next and the For Each...Next statements. (The repetition statements While, Do While...Loop and Do Until...Loop are covered in this chapter; the Do...Loop While, Do...Loop Until, and For...Next statements are covered in Chapter 6, Control Statements: Part 2; and the For Each...Next statement is covered in Chapter 8, Arrays.) The words If, Then, Else, End, Select, Case, While, Do, Until, Loop, For, Next and Each are all Visual Basic keywords (Fig. 3.2). Summary of Control Statements in Visual BasicVisual Basic has only three kinds of control structures, which from this point forward we refer to as control statements: the sequence structure, selection statements (three typesIf...Then, If...Then...Else and Select...Case) and repetition statements (seven typesWhile, Do While...Loop, Do...Loop While, Do Until...Loop, Do...Loop Until, For...Next and For Each...Next)for a total of 11 types of control statements. Every program is formed by combining as many of each type of control statement as is necessary. We can model each control statement as an activity diagram. Each diagram contains one initial state and one final state, which represent the control statement's entry point and exit point, respectively. These single-entry/single-exit control statements make it easy to build programsthe control statements are "attached" to one another by "connecting" the exit point of one control statement to the entry point of the next (which we accomplish simply by following one control statement immediately by another). This is similar to stacking building blocks, so we call it control-statement stacking. Section 5.10 shows an example of control-statement stacking. There is only one other way to connect control statements, and that is through control-statement nesting, where one control statement is placed inside another. We discuss control-statement nesting in Sections 5.11 and 5.12. Software Engineering Observation 5.1
|