Software Engineering for Students A Programming Approach
Software Engineering for Students A Programming Approach Software Engineering for Students A Programming Approach
> 14.7 Repetition 183 There is something of a controversy here. Some people argue that when a case statement is executed, the programmer should be completely aware of all the possibilities that can occur. So the default statement is redundant and just an invitation to be lazy and sloppy. Where necessary, the argument goes, a case statement should be preceded by if statements that ensure that only valid values are supplied to the case statement. if-not It would be reasonable to think that there would no longer be any controversy over language structures for selection. The if-else is apparently well established. However, the lack of symmetry in the if statement is open to criticism. While it is clear that the then part is carried out if the condition is true, the else part is rather tagged on at the end to cater for all other situations. Experimental evidence suggests that significantly fewer bugs arise if the programmer is required to restate the condition (in its negative form) prior to the else as shown below: if condition statement1 not condition else statement2 endif 14.7 ● Repetition Control structures for repetition traditionally fall into two classes. There are loop structures where the number of iterations is fixed, and those where the number of iterations is controlled by the evaluation of some condition. Fixed length iteration is often implemented using a form similar to that shown below: for control_variable = initial_expression to final_expression step step_expression do statement(s) endfor The usefulness and reliability of the for statement can be affected by a number of issues as now discussed Should the type of the loop control variable be limited to integers? Perhaps any ordinal type should be allowed. However, reals (floats) should not be allowed. For example, consider how many iterations are specified by the following: for x = 0.0 to 1.0 step 0.33 do Here it is not at all obvious exactly how many repetitions will be performed, and things are made worse by the fact that computers represent real values only approximately. > >
184 Chapter 14 ■ The basics > > > (Note how disallowing the use of reals as loop control variables conflicts with the aim of orthogonality). The semantics of the for is greatly affected by the answers to the following questions. When and how many times are the initial expression, final expression and step expressions evaluated? Can any of these expressions be modified within the loop? What is of concern here is whether or not it is clear how many iterations of the loop will be performed. If the expressions can be modified and the expressions are recomputed on each iteration, then there is a distinct possibility of producing an infinite loop. Similar problems arise if the loop control variable can be modified within the loop. The scope of the loop control variable is best limited to the for statement, as in Java. If it is not, then what should its value be on exit from the loop, or should it be undefined? Condition-controlled loops are simpler in form. Almost all modern languages provide a leading decision repetition structure (while-do) and some, for convenience, also provide a trailing decision form (repeat-until). while condition do repeat statement(s) statement(s) endwhile until condition The while form continues to iterate while a condition evaluates to true. Since the test appears at the head of the form, the while performs zero or many iterations of the loop body. The repeat, on the other hand, iterates until a condition is true. The test appears following the body of the loop, ensuring that the repeat performs at least one iteration. Thus the while statement is the more general looping mechanism of the two, so if a language provides only one looping mechanism, it should therefore be the while. However the repeat is sometimes more appropriate in some programming situations. and: SELF-TEST QUESTION 14.1 Identify a situation where repeat is more appropriate than while. Some languages provide the opposites of these two loops: do statement(s) while condition until condition do statement(s) end until > > >
- Page 156 and 157: 10.5 Structure clashes 133 Let us r
- 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: 182 Chapter 14 ■ The basics > Cas
- Page 209 and 210: 186 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
184 Chapter 14 ■ The basics<br />
><br />
><br />
><br />
(Note how disallowing the use of reals as loop control variables conflicts with the aim<br />
of orthogonality).<br />
The semantics of the <strong>for</strong> is greatly affected by the answers to the following questions.<br />
When and how many times are the initial expression, final expression and step<br />
expressions evaluated? Can any of these expressions be modified within the loop? What<br />
is of concern here is whether or not it is clear how many iterations of the loop will be<br />
per<strong>for</strong>med. If the expressions can be modified and the expressions are recomputed on<br />
each iteration, then there is a distinct possibility of producing an infinite loop. Similar<br />
problems arise if the loop control variable can be modified within the loop.<br />
The scope of the loop control variable is best limited to the <strong>for</strong> statement, as in<br />
Java. If it is not, then what should its value be on exit from the loop, or should it be<br />
undefined?<br />
Condition-controlled loops are simpler in <strong>for</strong>m. Almost all modern languages provide<br />
a leading decision repetition structure (while-do) and some, <strong>for</strong> convenience, also<br />
provide a trailing decision <strong>for</strong>m (repeat-until).<br />
while condition do repeat<br />
statement(s) statement(s)<br />
endwhile until condition<br />
The while <strong>for</strong>m continues to iterate while a condition evaluates to true. Since the<br />
test appears at the head of the <strong>for</strong>m, the while per<strong>for</strong>ms zero or many iterations of the<br />
loop body. The repeat, on the other hand, iterates until a condition is true. The test<br />
appears following the body of the loop, ensuring that the repeat per<strong>for</strong>ms at least one<br />
iteration. Thus the while statement is the more general looping mechanism of the two,<br />
so if a language provides only one looping mechanism, it should there<strong>for</strong>e be the while.<br />
However the repeat is sometimes more appropriate in some programming situations.<br />
and:<br />
SELF-TEST QUESTION<br />
14.1 Identify a situation where repeat is more appropriate than while.<br />
Some languages provide the opposites of these two loops:<br />
do<br />
statement(s)<br />
while condition<br />
until condition do<br />
statement(s)<br />
end until<br />
><br />
><br />
>