Software Engineering for Students A Programming Approach
Software Engineering for Students A Programming Approach Software Engineering for Students A Programming Approach
CHAPTER 7 Structured programming This chapter explains: ■ the principles of structured programming ■ the arguments surrounding the goto statement. 7.1 ● Introduction Structured programming is now part and parcel of most approaches to programming. It is widely accepted that it is not only the best, but the only way to carry out programming. This was not always the case. At one time there was a great debate about structured programming, what it meant and whether it was a good idea. This chapter reviews the principles and practice surrounding structured programming. Once upon a time most programming languages provided a goto statement that allowed control to be transferred to any desired place in a program. Here is an example of goto in action: ------ -----label: ------ -----goto label ------ ------ A label can be placed anywhere within a program. The goto statement transfers control to the specified label. The venerable language C provides a goto statement. Among modern languages, Java does not support a goto but C# does. This chapter seeks to answer questions like: What is the essence of structured programming? What is the role of the goto statement? Why are certain control structures favored?
88 Chapter 7 ■ Structured programming Figure 7.1 The three structures of structured programming One view of structured programming is that it holds that programs should only be built from three components: sequences (normally written in the order in which the statements are to be executed), selections (normally written as if-then-else), and repetitions (written as while-do). The goto statement is, by implication, banned. In this chapter we begin by examining the controversy about the goto statement. The outcome of the argument is that gotos are an irrelevancy; the argument is about something else, good program structure. We go on to explore the significant principles of structured programming. There are some other principles. We will explore these using flowcharts, which describe flow of control. A flowchart is read from the top downwards or in the direction of the arrows. Flowchart decisions (corresponding to if or while statements in code) are drawn as diamonds. Flowchart activities are shown as rectangular boxes. A flowchart is very similar to a UML activity diagram and conveys the same information. If the three structures of structured programming are diagrammed as flowcharts (Figure 7.1), the following characteristics become clear: 1. they have only one entry and exit 2. none of the constructs consists of more than three boxes Figure 7.2 A control structure that is not structured
- Page 60 and 61: 4.2 The concept of a requirement 37
- Page 62 and 63: 4.3 The qualities of a specificatio
- Page 64 and 65: 4.5 The requirements specification
- Page 66 and 67: 4.6 The structure of a specificatio
- Page 68 and 69: 4.7 ● Use cases 4.7 Use cases 45
- Page 70 and 71: Summary The ideal characteristics o
- Page 72: Further reading 49 Further reading
- Page 76 and 77: CHAPTER 5 This chapter explains: 5.
- Page 78 and 79: 5.3 Styles of human-computer interf
- Page 80 and 81: 5.5 Design principles and guideline
- Page 82 and 83: 5.5 Design principles and guideline
- Page 84 and 85: SELF-TEST QUESTION 5.2 What problem
- Page 86 and 87: 5.8 Help systems 63 Our plan is to
- Page 88 and 89: Further reading 65 5.5 Design a use
- Page 90 and 91: CHAPTER 6 Modularity This chapter e
- Page 92 and 93: 6.2 Why modularity? 69 observed fau
- Page 94 and 95: Figure 6.1 Two alternative software
- Page 96 and 97: ■ a simple program is more likely
- Page 98 and 99: 6.6 Information hiding 75 The class
- Page 100 and 101: 6.8 ● Coupling 6.8 Coupling 77 We
- Page 102 and 103: 6. Method calls with parameters tha
- Page 104 and 105: 3. Temporal cohesion 6.9 Cohesion 8
- Page 106 and 107: > } public void setY(int newY) { y
- Page 108 and 109: • Exercises 6.1 What is modularit
- Page 112 and 113: 7.2 Arguments against goto 89 If we
- Page 114 and 115: ■ if-then-else ■ while-do or re
- Page 116 and 117: 7.3 Arguments in favor of goto 93 l
- Page 118 and 119: 7.4 Selecting control structures 95
- Page 120 and 121: while do if endif then else endWhil
- Page 122 and 123: • Exercises 7.1 Review the argume
- Page 124 and 125: count = 0 loop: count = count + 1 i
- Page 126 and 127: > 8.2 Case study 103 A statement th
- Page 128 and 129: start button event create defender
- Page 130 and 131: 8.3 ● Discussion Abstraction One
- Page 132 and 133: Exercises 109 skill. On the other h
- Page 134 and 135: CHAPTER 9 This chapter explains: 9.
- Page 136 and 137: 9.2 Identifying data flows 113 Noti
- Page 138 and 139: 9.3 Creation of a structure chart 1
- Page 140 and 141: SELF-TEST QUESTION 9.4 Discussion 1
- Page 142 and 143: Exercises 119 During the second sta
- Page 144 and 145: CHAPTER 10 This chapter explains:
- Page 146 and 147: In English, this reads: 10.2 A simp
- Page 148 and 149: 10.2 A simple example 125 Now comes
- Page 150 and 151: 10.4 Multiple input and output stre
- Page 152 and 153: Process header Process issue 10.4 M
- Page 154 and 155: 10.5 Structure clashes 131 As seen
- Page 156 and 157: 10.5 Structure clashes 133 Let us r
- Page 158 and 159: 10.6 Discussion 135 ■ teachable -
CHAPTER<br />
7 Structured<br />
programming<br />
This chapter explains:<br />
■ the principles of structured programming<br />
■ the arguments surrounding the goto statement.<br />
7.1 ● Introduction<br />
Structured programming is now part and parcel of most approaches to programming.<br />
It is widely accepted that it is not only the best, but the only way to carry out programming.<br />
This was not always the case. At one time there was a great debate about<br />
structured programming, what it meant and whether it was a good idea. This chapter<br />
reviews the principles and practice surrounding structured programming.<br />
Once upon a time most programming languages provided a goto statement that<br />
allowed control to be transferred to any desired place in a program. Here is an example<br />
of goto in action:<br />
------<br />
-----label:<br />
------<br />
-----goto<br />
label<br />
------<br />
------<br />
A label can be placed anywhere within a program. The goto statement transfers control<br />
to the specified label. The venerable language C provides a goto statement. Among<br />
modern languages, Java does not support a goto but C# does.<br />
This chapter seeks to answer questions like: What is the essence of structured programming?<br />
What is the role of the goto statement? Why are certain control structures<br />
favored?