- 4.1 Introduction
- 4.2 Algorithms
- 4.3 Pseudocode
- 4.4 Control Structures
- 4.5 If...Then Selection Statement
- 4.6 If...Then...Else Selection Statement
- 4.7 Nested If...Then...Else Statements
- 4.8 Repetition Statements
- 4.9 Compound Assignment Operators
- 4.10 Formulating Algorithms: Counter-Controlled Repetition
- 4.11 Formulating Algorithms: Nested Control Statements
- 4.12 Using the Debugger: Locating a Logic Error
- 4.13 Wrap-Up
- Self-Review Exercises
- Answers to Self-Review Exercises
- Quick Quiz
- Making a Difference Exercises
4.4 Control Structures
Normally, statements in a program are executed one after another in the order in which they're written. This is called sequential execution. A transfer of control occurs when an executed statement does not directly follow the previously executed statement.
During the 1960s, it became clear that the indiscriminate use of transfers of control was causing difficulty for software development groups. A key problem was the GoTo statement, which allows you to specify a transfer of control to one of a wide range of possible destinations in a program. The excessive use of GoTo statements caused programs to become quite unstructured and hard to follow—such disorganized programs were referred to as "spaghetti code." Since then, the notion of structured programming has become almost synonymous with "GoTo elimination."
Research demonstrated that all programs could be written in terms of only three control structures—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'll refer to them as "control statements."
Unless directed otherwise, the computer executes statements sequentially. The UML activity diagram in Fig. 4.1 illustrates a sequence structure that performs two calculations in order. You can have as many actions as you want in a sequence structure. Anywhere a single action may be placed, you may place several actions in sequence.
Fig. 4.1 Sequence-structure activity diagram.
The two statements in Fig. 4.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.
UML Activity Diagrams
Like pseudocode, activity diagrams help you develop and represent algorithms. 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. 4.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), the diamond symbol and the small circle symbol; these symbols are connected by transition arrows, which represent the flow of the activity.
Activity diagrams clearly show how control structures operate. The sequence-structure activity diagram in Fig. 4.1 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 arrows represent transitions that indicate the order in which the actions occur—the program that implements the activities illustrated by the activity diagram in Fig. 4.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 state—the beginning of the workflow before the corresponding program performs the activities. The solid circle surrounded by a hollow circle at the bottom of the activity diagram represents the final state—the end of the workflow after the corresponding program performs its activities.
Figure 4.1 also includes rectangles with the upper-right corners folded over. These are called notes in the UML. Notes are optional comments that describe the purpose of symbols in the diagram. A dotted line connects each note with the element the note describes.
Each activity diagram contains one initial state and one final state, which represent the control statement's entry and exit point, respectively. These single-entry/single-exit control statements make it easy to build programs—the 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. 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.
Visual Basic provides three types of selection statements.
- The If ... Then selection statement 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 statement is called a single-selection statement because it selects or ignores a single action (or a sequence of actions).
- The If ... Then ... Else selection statement 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 If...Then...Else statement is called a double-selection statement because it selects between two different actions (or sequences of actions).
- The Select...Case selection statement performs one of many possible actions (or sequences of actions), depending on the value of an expression. For this reason, the Select...Case statement is called a multiple-selection statement.
Visual 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 Do While...Loop and While...End While repetition statements execute a set of statements while a condition—known as the loop-continuation condition—remains true. If the condition is initially false, the set of statements does not execute.
- The Do Until...Loop repetition statement executes a set of statements until a condition—known as the loop-termination condition—becomes true. If the condition is initially true, the set of statements does not execute.
- The Do...Loop While repetition statement executes a set of statements while its loop-continuation condition remains true. The set of statements is guaranteed to execute at least once.
- The Do...Loop Until repetition statement executes a set of statements until its loop-termination condition becomes true. The set of statements is guaranteed to execute at least once.
- The For...Next repetition statement executes a set of statements a specified number of times—this is known as counter-controlled (or definite) repetition.
- The For Each...Next repetition statement (introduced in Chapter 7) performs a set of statements for every element of a so-called array or collection of values.
The words If, Then, Else, End, Select, Case, While, Do, Until, Loop, For, Next and Each are all keywords. By providing many repetition statements, the designers of Visual Basic make it more convenient for you to express certain types of algorithms. Theoretically, however, you need only one repetition statement that enables you to loop zero or more times based on the truth or falsity of a condition—both Do While...Loop and While...End While allow you to do this. In this book, we typically use the Do While...Loop, For...Next and For Each... Next repetition statements.