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

• Exercises 7.1 Review the arguments for and against goto statements and criticize their validity. Exercises 99 4. the difficulty of finding out where you came from in a program constructed using gotos 5. the need to read backwards in a program with gotos in it. 6. the difficulty of proving the correctness of programs that use gotos. The arguments for gotos are: 1. to ban them is to deskill programming 2. they have a use in exceptional circumstances 3. gotos are sometimes necessary in order to make programs perform well 4. it is sometimes “natural” to use gotos. Much of the debate about structured programming has been conducted in this way. Throughout the arguments, the need for clarity in programs is the dominant idea. The avoidance of gotos does not necessarily lead to a structured program. The central idea of structured programming is abstraction. Indeed, we can define structured programming as: “the systematic use of (control) abstraction in programming” (Abstraction can be applied not just to control, but to data as well. This is the subject of other chapters.) Abstraction requires not only the use of suitable control structures but also a systematic design method. 7.2 State in your own words what structured programming is. Imagine that you meet a friend who is used to programming using gotos. Explain in simple clear language what structured programming is. 7.3 A while statement can be used instead of an if statement, because the statement: if bool then s endif can be rewritten as: while bool do s bool = false endwhile Show how an if-then-else statement can similarly be rewritten using while statements. If the if statement is superfluous, why have it?

100 Chapter 7 ■ Structured programming > > 7.4 Convert the following into a structured program. i = start loop: if x = a[i] then goto found endif if i = end then goto notFound endif i = i + 1 goto loop notFound: display 'not found' action1 goto end found: display 'found' action2 end: Compare and contrast the two programs. 7.5 Recursion can be used to accomplish repetition, as in the following example of a method to skip spaces in a stream of input information: method skipSpaces read(char) if char = space then skipSpaces endif endmethod Convert this example to a method that uses a while statement for repetition. Compare and contrast the two solutions. What is the role of recursion in the programmer’s toolkit? 7.6 Argue for and against providing the following constructs in a programming language: ■ case ■ repeat-until ■ until-do Answer to self-test question 7.1 count = 0 while count < 10 do count = count + 1 endwhile > >

•<br />

Exercises<br />

7.1 Review the arguments <strong>for</strong> and against goto statements and criticize their validity.<br />

Exercises 99<br />

4. the difficulty of finding out where you came from in a program constructed<br />

using gotos<br />

5. the need to read backwards in a program with gotos in it.<br />

6. the difficulty of proving the correctness of programs that use gotos.<br />

The arguments <strong>for</strong> gotos are:<br />

1. to ban them is to deskill programming<br />

2. they have a use in exceptional circumstances<br />

3. gotos are sometimes necessary in order to make programs per<strong>for</strong>m well<br />

4. it is sometimes “natural” to use gotos.<br />

Much of the debate about structured programming has been conducted in this way.<br />

Throughout the arguments, the need <strong>for</strong> clarity in programs is the dominant idea.<br />

The avoidance of gotos does not necessarily lead to a structured program. The<br />

central idea of structured programming is abstraction. Indeed, we can define structured<br />

programming as:<br />

“the systematic use of (control) abstraction in programming”<br />

(Abstraction can be applied not just to control, but to data as well. This is the subject<br />

of other chapters.)<br />

Abstraction requires not only the use of suitable control structures but also a systematic<br />

design method.<br />

7.2 State in your own words what structured programming is. Imagine that you meet a<br />

friend who is used to programming using gotos. Explain in simple clear language what<br />

structured programming is.<br />

7.3 A while statement can be used instead of an if statement, because the statement:<br />

if bool then s endif<br />

can be rewritten as:<br />

while bool do s bool = false endwhile<br />

Show how an if-then-else statement can similarly be rewritten using while statements.<br />

If the if statement is superfluous, why have it?

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

Saved successfully!

Ooh no, something went wrong!