Software Engineering for Students A Programming Approach
Software Engineering for Students A Programming Approach Software Engineering for Students A Programming Approach
> 14.7 Repetition 185 C, C++, C# and Java all provide while-do and do-while structures. They also provide a type of for statement that combines together several commonly used ingredients. An example of this loop structure is: for (i = 0; i < 10; i++) { statement(s) } in which: ■ the first statement within the brackets is done once, before the loop is executed ■ the second item, a condition, determines whether the loop will continue ■ the third statement is executed at the end of each repetition. We will meet yet another construct for repetition – the foreach statement – in the chapter on object-oriented programming language features (Chapter 15). This is convenient for processing all the elements of a data structure. The while and repeat structures are satisfactory for the vast majority of iterations we wish to specify. For the most part, loops which terminate at either their beginning or end are sufficient. However, there are situations, notably when encountering some exceptional condition, where it is appropriate to be able to branch out of a repetition structure at an arbitrary point within the loop. Sometimes it is necessary to break out of a series of nested loops rather than a single loop. In many languages, the programmer is limited to two options. The terminating conditions of each loop can be enhanced to accommodate the “exceptional” exit, and if statements can be used within the loop to transfer control to the end of the loop should the exceptional condition occur. This solution is clumsy at best and considerably decreases the readability of the code. A second, and arguably better, solution is to use the much-maligned goto statement to branch directly out of the loops. Ideally, however, since there is a recognized need for n and a half times loops, the language should provide a controlled way of exiting from one or more loops. Java provides the following facility where an orderly break may be made but only to the statement following the loop(s). while (condition) { statement(s) if (condition) break; statement(s) } In the example above, control will be transferred to the statement following the loop when condition is true. This may be the only way of exiting from this loop. here: while (condition) { while (condition) { > > >
186 Chapter 14 ■ The basics > } } statement(s) if (exitCondition) break here; statement(s) In the second example above, control will be transferred out of both while loops when exitCondition is true. Note how the outer while loop is labeled here: and how this label is used by the if statement to specify that control is to be transferred to the end of the while loop (not the beginning) when exitCondition is satisfied. SELF-TEST QUESTION 14.2 Sketch out the code for a method to search an array of integers to find some desired integer. Write two versions – one using the break mechanism and one without break. The languages C, C++, Ada and Java provide a mechanism such as the above for breaking out in the middle of loops. There is some controversy about using break statements. Some people argue that it is simply too much like the notorious goto statement. There is a difference, however, because break can only be used to break out of a loop, not enter into a loop. Neither can break be used to break out of an if statement. Thus it might be argued that break is a goto that is under control. Handling errors or exceptional situations is a common programming situation. In the past, such an eventuality was handled using the goto statement. Nowadays features are built in to programming languages to facilitate the more elegant handling of such situations. We discuss the handling of exceptions in Chapter 17. 14.8 ● Methods Procedural or algorithmic abstraction is one of the most powerful tools in the programmer’s arsenal. When designing a program, we abstract what should be done before we specify how it should be done. Before OOP, program designs evolved as layers of procedural abstractions, each layer specifying more detail than the layer above. Procedural abstractions in programming languages, such as procedures and functions, allow the layered design of a program to be accurately reflected in the structure of the program text. Even in relatively small programs, the ability to factor a program into small, functional modules is essential; factoring increases the readability and maintainability of programs. What does the software engineer require from a language in terms of support for procedural abstraction? We suggest the
- Page 158 and 159: 10.6 Discussion 135 ■ teachable -
- Page 160 and 161: Exercises 137 2. a control block, s
- Page 162 and 163: CHAPTER 11 Object-oriented design T
- Page 164 and 165: Figure 11.1 The cyberspace invaders
- Page 166 and 167: SELF-TEST QUESTION 11.1 Derive info
- Page 168 and 169: 11.5 Class-responsibility-collabora
- Page 170 and 171: 11.7 ● Discussion Summary 147 OOD
- Page 172 and 173: 11.11 Compare and contrast the prin
- Page 174 and 175: CHAPTER 12 This chapter explains: 1
- Page 176 and 177: 12.3 Delegation 153 The concepts of
- Page 178 and 179: 12.5 Factory method 155 The followi
- Page 180 and 181: 12.8 Model, view controller (observ
- Page 182 and 183: Figure 12.4 Pipe and Filter pattern
- Page 184 and 185: Figure 12.6 Layers in a distributed
- Page 186 and 187: Answers to self-test questions 163
- Page 188 and 189: CHAPTER 13 Refactoring This chapter
- Page 190 and 191: 13.3 ● Move Method 13.6 Inline Cl
- Page 192 and 193: class Sprite Instance variables x y
- Page 194 and 195: Summary Summary 171 it is making po
- Page 196: PART C PROGRAMMING LANGUAGES
- Page 199 and 200: 176 Chapter 14 ■ The basics and a
- Page 201 and 202: 178 Chapter 14 ■ The basics > > >
- Page 203 and 204: 180 Chapter 14 ■ The basics > Ear
- Page 205 and 206: 182 Chapter 14 ■ The basics > Cas
- Page 207: 184 Chapter 14 ■ The basics > > >
- Page 211 and 212: 188 Chapter 14 ■ The basics Unfor
- Page 213 and 214: 190 Chapter 14 ■ The basics Ada d
- Page 215 and 216: 192 Chapter 14 ■ The basics The w
- Page 217 and 218: 194 Chapter 14 ■ The basics In a
- Page 219 and 220: 196 Chapter 14 ■ The basics > str
- Page 221 and 222: 198 Chapter 14 ■ The basics Answe
- Page 223 and 224: CHAPTER 15 Object-oriented programm
- Page 225 and 226: 202 Chapter 15 ■ Object-oriented
- Page 227 and 228: 204 Chapter 15 ■ Object-oriented
- Page 229 and 230: 206 Chapter 15 ■ Object-oriented
- Page 231 and 232: 208 Chapter 15 ■ Object-oriented
- Page 233 and 234: 210 Chapter 15 ■ Object-oriented
- Page 235 and 236: 212 Chapter 15 ■ Object-oriented
- Page 237 and 238: 214 Chapter 15 ■ Object-oriented
- Page 239 and 240: 216 Chapter 15 ■ Object-oriented
- Page 241 and 242: 218 Chapter 15 ■ Object-oriented
- Page 243 and 244: 220 Chapter 15 ■ Object-oriented
- Page 245 and 246: 222 Chapter 16 ■ Programming in t
- Page 247 and 248: 224 Chapter 16 ■ Programming in t
- Page 249 and 250: 226 Chapter 16 ■ Programming in t
- Page 251 and 252: 228 Chapter 16 ■ Programming in t
- Page 253 and 254: 230 Chapter 16 ■ Programming in t
- Page 255 and 256: 232 Chapter 16 ■ Programming in t
- Page 257 and 258: 234 Chapter 16 ■ Programming in t
186 Chapter 14 ■ The basics<br />
><br />
}<br />
}<br />
statement(s)<br />
if (exitCondition) break here;<br />
statement(s)<br />
In the second example above, control will be transferred out of both while loops<br />
when exitCondition is true. Note how the outer while loop is labeled here: and<br />
how this label is used by the if statement to specify that control is to be transferred to<br />
the end of the while loop (not the beginning) when exitCondition is satisfied.<br />
SELF-TEST QUESTION<br />
14.2 Sketch out the code <strong>for</strong> a method to search an array of integers to find<br />
some desired integer. Write two versions – one using the break mechanism<br />
and one without break.<br />
The languages C, C++, Ada and Java provide a mechanism such as the above <strong>for</strong><br />
breaking out in the middle of loops.<br />
There is some controversy about using break statements. Some people argue that it<br />
is simply too much like the notorious goto statement. There is a difference, however,<br />
because break can only be used to break out of a loop, not enter into a loop. Neither<br />
can break be used to break out of an if statement. Thus it might be argued that<br />
break is a goto that is under control.<br />
Handling errors or exceptional situations is a common programming situation. In<br />
the past, such an eventuality was handled using the goto statement. Nowadays features<br />
are built in to programming languages to facilitate the more elegant handling of such<br />
situations. We discuss the handling of exceptions in Chapter 17.<br />
14.8 ● Methods<br />
Procedural or algorithmic abstraction is one of the most powerful tools in the programmer’s<br />
arsenal. When designing a program, we abstract what should be done<br />
be<strong>for</strong>e we specify how it should be done. Be<strong>for</strong>e OOP, program designs evolved as<br />
layers of procedural abstractions, each layer specifying more detail than the layer<br />
above. Procedural abstractions in programming languages, such as procedures and<br />
functions, allow the layered design of a program to be accurately reflected in the<br />
structure of the program text. Even in relatively small programs, the ability to factor<br />
a program into small, functional modules is essential; factoring increases the readability<br />
and maintainability of programs. What does the software engineer require<br />
from a language in terms of support <strong>for</strong> procedural abstraction? We suggest the