Software Engineering for Students A Programming Approach

Software Engineering for Students A Programming Approach Software Engineering for Students A Programming Approach

web.firat.edu.tr
from web.firat.edu.tr More from this publisher
21.08.2013 Views

> 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 > > >

><br />

14.7 Repetition 183<br />

There is something of a controversy here. Some people argue that when a case statement<br />

is executed, the programmer should be completely aware of all the possibilities that<br />

can occur. So the default statement is redundant and just an invitation to be lazy and<br />

sloppy. Where necessary, the argument goes, a case statement should be preceded by<br />

if statements that ensure that only valid values are supplied to the case statement.<br />

if-not<br />

It would be reasonable to think that there would no longer be any controversy over language<br />

structures <strong>for</strong> selection. The if-else is apparently well established. However,<br />

the lack of symmetry in the if statement is open to criticism. While it is clear that the<br />

then part is carried out if the condition is true, the else part is rather tagged on at the<br />

end to cater <strong>for</strong> all other situations. Experimental evidence suggests that significantly<br />

fewer bugs arise if the programmer is required to restate the condition (in its negative<br />

<strong>for</strong>m) prior to the else as shown below:<br />

if condition<br />

statement1<br />

not condition else<br />

statement2<br />

endif<br />

14.7 ● Repetition<br />

Control structures <strong>for</strong> repetition traditionally fall into two classes. There are loop structures<br />

where the number of iterations is fixed, and those where the number of iterations<br />

is controlled by the evaluation of some condition. Fixed length iteration is often implemented<br />

using a <strong>for</strong>m similar to that shown below:<br />

<strong>for</strong> control_variable =<br />

initial_expression to final_expression step step_expression<br />

do<br />

statement(s)<br />

end<strong>for</strong><br />

The usefulness and reliability of the <strong>for</strong> statement can be affected by a number of<br />

issues as now discussed<br />

Should the type of the loop control variable be limited to integers? Perhaps any ordinal<br />

type should be allowed. However, reals (floats) should not be allowed. For example,<br />

consider how many iterations are specified by the following:<br />

<strong>for</strong> x = 0.0 to 1.0 step 0.33 do<br />

Here it is not at all obvious exactly how many repetitions will be per<strong>for</strong>med, and things<br />

are made worse by the fact that computers represent real values only approximately.<br />

><br />

>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!