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

CHAPTER 7 Structured programming This chapter explains: ■ the principles of structured programming ■ the arguments surrounding the goto statement. 7.1 ● Introduction Structured programming is now part and parcel of most approaches to programming. It is widely accepted that it is not only the best, but the only way to carry out programming. This was not always the case. At one time there was a great debate about structured programming, what it meant and whether it was a good idea. This chapter reviews the principles and practice surrounding structured programming. Once upon a time most programming languages provided a goto statement that allowed control to be transferred to any desired place in a program. Here is an example of goto in action: ------ -----label: ------ -----goto label ------ ------ A label can be placed anywhere within a program. The goto statement transfers control to the specified label. The venerable language C provides a goto statement. Among modern languages, Java does not support a goto but C# does. This chapter seeks to answer questions like: What is the essence of structured programming? What is the role of the goto statement? Why are certain control structures favored?

88 Chapter 7 ■ Structured programming Figure 7.1 The three structures of structured programming One view of structured programming is that it holds that programs should only be built from three components: sequences (normally written in the order in which the statements are to be executed), selections (normally written as if-then-else), and repetitions (written as while-do). The goto statement is, by implication, banned. In this chapter we begin by examining the controversy about the goto statement. The outcome of the argument is that gotos are an irrelevancy; the argument is about something else, good program structure. We go on to explore the significant principles of structured programming. There are some other principles. We will explore these using flowcharts, which describe flow of control. A flowchart is read from the top downwards or in the direction of the arrows. Flowchart decisions (corresponding to if or while statements in code) are drawn as diamonds. Flowchart activities are shown as rectangular boxes. A flowchart is very similar to a UML activity diagram and conveys the same information. If the three structures of structured programming are diagrammed as flowcharts (Figure 7.1), the following characteristics become clear: 1. they have only one entry and exit 2. none of the constructs consists of more than three boxes Figure 7.2 A control structure that is not structured

CHAPTER<br />

7 Structured<br />

programming<br />

This chapter explains:<br />

■ the principles of structured programming<br />

■ the arguments surrounding the goto statement.<br />

7.1 ● Introduction<br />

Structured programming is now part and parcel of most approaches to programming.<br />

It is widely accepted that it is not only the best, but the only way to carry out programming.<br />

This was not always the case. At one time there was a great debate about<br />

structured programming, what it meant and whether it was a good idea. This chapter<br />

reviews the principles and practice surrounding structured programming.<br />

Once upon a time most programming languages provided a goto statement that<br />

allowed control to be transferred to any desired place in a program. Here is an example<br />

of goto in action:<br />

------<br />

-----label:<br />

------<br />

-----goto<br />

label<br />

------<br />

------<br />

A label can be placed anywhere within a program. The goto statement transfers control<br />

to the specified label. The venerable language C provides a goto statement. Among<br />

modern languages, Java does not support a goto but C# does.<br />

This chapter seeks to answer questions like: What is the essence of structured programming?<br />

What is the role of the goto statement? Why are certain control structures<br />

favored?

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

Saved successfully!

Ooh no, something went wrong!