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

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

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

Saved successfully!

Ooh no, something went wrong!