Vous êtes sur la page 1sur 10

structured programming

Home > Midmarket CIO Definitions - Structured programming

E
M
AI
Top of Form L
TH Bottom of
IS Form
Bott
om
of
For
m
SearchCIO-Midmarket.com
Definitions (Powered by
WhatIs.com)

LOOK UP TECH TERMS Powered by:

Top of Form Browse tech terms alphabetically:


Search listings for thousands of IT
terms: ABCDEFGHIJKLMNOPQRSTU
VWXYZ#

Bottom of Form

structured programming
Mid-market
CIO
structured
programmin
Show me everything on CIO-Midmarket Resources
g Research
and Reports

- Structured programming (sometimes known as modular


programming) is a subset of procedural programming that enforces a
logical structure on the program being written to make it more efficient and
easier to understand and modify. Certain languages such as Ada, Pascal,
and dBASE are designed with features that encourage or enforce a logical
program structure.
Structured programming frequently employs a top-down design model, in
which developers map out the overall program structure into separate
subsections. A defined function or set of similar functions is coded in a
separate module or submodule, which means that code can be loaded into
memory more efficiently and that modules can be reused in other programs.
After a module has been tested individually, it is then integrated with other
modules into the overall program structure.
Program flow follows a simple hierarchical model that employs looping
constructs such as "for," "repeat," and "while." Use of the "Go To"
statement is discouraged.
Structured programming was first suggested by Corrado Bohm and
Guiseppe Jacopini. The two mathematicians demonstrated that any
computer program can be written with just three structures: decisions,
sequences, and loops. Edsger Dijkstra's subsequent article, Go To Statement
Considered Harmful was instrumental in the trend towards structured
programming. The most common methodology employed was developed
by Dijkstra. In this model (which is often considered to be synonymous
with structured programming, although other models exist) the developer
separates programs into subsections that each have only one point of access
and one point of exit.
Almost any language can use structured programming techniques to avoid
common pitfalls of unstructured languages. Unstructured programming
must rely upon the discipline of the developer to avoid structural problems,
and as a consequence may result in poorly organized programs. Most
modern procedural languages include features that encourage structured
programming. Object-oriented programming (OOP) can be thought of as a
type of structured programming, uses structured programming techniques
for program flow, and adds more structure for data to the model.
LAST UPDATED: 31 Aug 2005

Do you have something to add to this definition? Let us


know.
Send your comments to techterms@whatis.com
More resources from around the web:
- Wikipedia offers an entry about structured programming.
- Dijkstra's classic paper, "Go To Statement Considered Harmful," is
available online.

'); $(document).ready(function() { if(cl_exclusive != 'Y' && !isSF) $


('#googleAdSenseFooter').writeCapture().html(''); }); // -->

FILE EXTENSION AND FILE FORMAT LIST


File Extension and File Format List:
ABCDEFGHIJKLMNOPQRSTUVWXYZ#
RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
Bcc (SearchCIO-Midmarket.com)
business activity monitoring (SearchCIO-Midmarket.com)
Business activity monitoring (BAM), also called business activity
management, is the use of technology to proactively define and
analyze critical...

Top of
Form
About Us | Contact Us | For Advertisers | For Business SEARC Bottom of
Partners | Site Index | RSS H Form

TechTarget provides technology professionals with the information they need to perform their jobs - from
developing strategy, to making cost-effective purchase decisions and managing their organizations' technology
projects - with its network of technology-specific websites, events and online magazines.
TechTarget Corporate Web Site | Media Kits | Reprints | Site Map

All Rights Reserved, Copyright 2007 - 2010, TechTarget | Read our Privacy
Policy

Introduction
One of the most important concepts of programming is the ability to control a program so that
different lines of code are executed or that some lines of code are executed many times. The
mechanisms that allow us to control the flow of execution are called control structures.
Flowcharting is a method of documenting (charting) the flow (or paths) that a program would
execute. There are four main categories of control structures:
• Sequence – Very boring. Simply do one instruction then the next and the
next. Just do them in a given sequence or in order listed. Most lines of
code are this.
• Selection – This is where you select or choose between two or more flows.
The choice is decided by asking some sort of question. The answer
determines the path (or which lines of code) will be executed.
• Iteration – Also known as repetition, it allows some code (one too many
lines) to be executed (or repeated) several times. The code might not be
executed at all (repeat it zero times), executed a fixed number of times or
executed indefinitely until some condition has been met. Also known as
looping because the flowcharting shows the flow looping back to repeat
the task.
• Branching – A control structure that allows the flow of execution to jump to
a different part of the program. This category is rarely used in modular
structured programming.
All high-level programming languages have control structures. All languages have the first
three categories of control structures (sequence, selection, and iteration). Most have the if
then else structure (which belongs to the selection category) and the while structure (which
belongs to the iteration category). After these two basic structures there are usually language
variations.
The concept of structured programming started in the late 1960's with an article by Edsger
Dijkstra. He proposed a "go to less" method of planning programming logic that eliminated
the need for the branching category of control structures. The topic was debated for about 20
years. But ultimately – "By the end of the 20th century nearly all computer scientists were
convinced that it is useful to learn and apply the concepts of structured programming. "1
Introduction to Selection Control Structures
The basic attribute of a selection control structure is to be able to select between two of more
alternate paths. This is described as either two-way selection or multiway selection. A
question using Boolean concepts usually controls which path is selected. All of the paths
from a selection control structure join back up at the end of the control structure, before
moving on to the next lines of code in a program.
We have mentioned that the if then else control structure belongs to the selection category
and is a two-way selection.
Example 1: if then else control structure

if (age > 17)


{
out << "You can vote.";
}
else
{
cout << "You can't vote.";
}

Introduction to Iteration Control Structures


The basic attribute of an iteration control structure is to be able to repeat some lines of code.
The visual display of iteration creates a circular loop pattern when flowcharted, thus the word
"loop" is associated with iteration control structures. Iteration can be accomplished with test
before loops, counting loops, and test after loops. A question using Boolean concepts usually
controls how long the loop will execute.
We have mentioned that the while control structure belongs to the iteration category and is a
test before loop.
Example 2: while control structure

counter = 0;
while (counter < 5)
{
cout << "\nI love computers!";
counter ++;
}

Definitions
Definition 1: control structures

Mechanisms that allow us to control the flow of execution within a


program.

Definition 2: sequence

A control structure where you do the items in the sequence listed.

Definition 3: selection

A control structure where you select between two or more choices.

Definition 4: iteration

A control structure that allows some lines of code to be executed many


times.

Definition 5: branching

A control structure that allows the flow of execution to jump to a different


part of the program.
Definition 6: structured programming

A method of planning programs that avoids the branching category of


control structures.

Structured Programming
"Structured programming" is programming that links control flow blocks (a
function, an if statement's block, etc.) to the scopes of variables. A variable
declared inside such a block is invisible outside it.

This leads to programs written with only the following code structures:

1. Sequence of sequentially executed statements.


2. Conditional execution of statements (i.e., "if" statements).
3. Looping.
4. Structured SubRoutine calls (e.g., 'gosub' but not 'goto').
5. StepwiseRefinement
In a "CargoCult" application of structured programming, the following concepts
are sometimes "forbidden":

• "GoTo" statements (except when used in a primitive language to


implement #1 to #3 above).
• "break" or "continue" out of the middle of loops.
• Multiple exit points to a function/procedure/subroutine (i.e., multiple
"return" statements; see SingleFunctionExitPoint).
• Multiple entry points to a function/procedure/subroutine (see below).
In practice, what should actually be forbidden is long, run-on functions. That rule
actually fixes the problems the previous rules feebly attempt to fix!

In general, Structured Programming is contrasted with SpaghettiCode, "code that


has no simple direct logical structure." When StructuredProgramming was
introduced, it was in response to SpaghettiCode problems caused by popular
languages and methodologies of the day.

Structured Programming is commonly mistaken as Procedural Programming,


altought, it started with Procedural Programming, but it can be applied to other
Programming Paradigms (Example: Object Oriented Programming). Many
applications described as coded with Structured Programming, are really built
with a Procedural Programming Language, that may be full, partially or none
Structured.

Structured Programming is not ProgrammingWithStructures.


Structured Programming is a foundation of ModularProgramming and
ObjectOrientedProgramming, as it's assumed that individual methods are
structured (i.e., coded with only #1 to #3 above). (Of course, plenty of people
write garbage in ObjectOrientedProgrammingLanguages.) (Yes, but it's
structured garbage!) (Not necessarily, but it's encapsulated within the object.)

"...it just seems like good programming practice to me." -- ChuckMoore, on one
of Dijkstra's papers on StructuredProgramming

CeeLanguage/CeePlusPlus programmers often casually violate the rules of


Structured Programming in minor ways, and rarely suffer major problems from it,
if their programs are otherwise modular or OO. Structured Programming is a non-
issue in the SmalltalkLanguage, as methods are so small that no rational person
would use "goto," even if the language supported it. (However, multiple exit
points are supported and frequently used in Smalltalk, so Smalltalk programs are
rarely "structured".) -- JeffGrigg

But aren't those forbidden constructs exactly what exceptions give us?
Alternatively, aren't most of the justifiable uses of GoTo in CeeLanguage to do
with simulating exceptions?

I think the answer is that the one additional structure missing from
PascalLanguage is abrupt termination on errors, and exceptions add this. --
MartinPool

I would say that exceptions violate the rules of structured programming. But go
ahead and use them: they're a good way to handle unusual
ExceptionalConditions. Any program written with exceptions could have been
written without them, using only the constructs (#1 to #3) above. But I think the
program with exceptions is likely to be more easily understood and maintained
by humans. (Like AllPanaceasBecomePoison.) -- JeffGrigg

My view is that structured programming is the opposite of ad hoc programming.


In structured programming, we shift program control in a very limited, prescribed
set of ways. By providing a highly controlled manner in which they operate,
exceptions would fit with this concept of structured programming. The point is
not that "GoTo is bad," the point is that "unrestricted use of go to is bad." --
WayneMack

NassiShneidermanDiagrams support StructuredProgramming, like a FlowChart


where forbidden constructs are impossible to express. (NewSpeak for
programmers. ;-)

JacksonStructuredProgramming (JSP) and JacksonStructuredDesign? (JSD) also


fall within the structured programming philosophy.
If multiple exits are bad, does this mean we shouldn't use GuardClauses?

Multiple exits aren't bad. This is an old and outdated paradigm.


StructuredProgramming, taken to an extreme or pure is some of the nastiest
code I've ever had to read. It's amazing what people will do to avoid a perfectly
reasonable break or early return. JMTC.

GuardClauses do not require multiple exits; they can be implemented either way.
[Note: This is merely intended as a clarification of guard clauses and does not
imply a position regarding multiple exits.]

As with everything, good ideas are quickly perverted. The need for structured
programming stemmed from the total lack of understanding of visual processing
at the time. Providing visual structure to your code is a GoodThing that will
speed up development and maintenance. Indentation and color highlighting are
two examples.

A shop a friend of mine had a manager ('way back in '70s) that thought that
since CobolLanguage was EnglishLanguage, paragraphs should be paragraphs.
Much like in the newspaper. Try reading that; it was a BadThing.

The idea behind removing GoTos was that there is no visual (untextual) cue as to
where you're going.

And just because a language is syntactically structured, doesn't mean code for it
is visually structured. If we are truly more concerned about human legibility than
machine input, then all code should be very pretty and properly indented. Look
at a lot of C/C++/JavaLanguage/./.. code and tell me it's structured for the
human reader.

It should be noted that different people mean different things by


StructuredProgramming. For instance DonKnuth famously wrote a paper called
StructuredProgrammingWithGoToStatements which argued for where and when
GoTo fit with structured programming.

See also InternalLoopExitsAreOk. (Yeah, tooting my own horn, so sue me.) --


BenTilly

The last bullet point used to read

• Multiple entry points to a function/procedure/subroutine. (...which is hard


to do outside AssemblyLanguage. ;-)
But it isn't all that hard, actually. FORTRAN (FortranLanguage) and PL/I
(PliLanguage) explicitly supported multiple entry points to a procedure, and this
mechanism was exploited in early approaches to ModularProgramming
(BarbaraLiskov taught her students this technique). Nowadays we use
complimentary [complementary?] names like 'PolyMorphism' and 'overloading'
for this sort of thing, but that's all that methods are: multiple entry points into
the same (virtual) procedure.

Multiple entry points can be done in any language that supports labels and GoTo
commands; this includes a lot more languages than just AssemblyLanguage.

Yes, but the point is that modern interpretations of multiple-entry are an


essential piece of current BestPractices. To repeat what was written in the
discussion about multiple exit points, structured programming is an old and
outmoded paradigm. It is a kind of CyberFundamentalism, that says "Stick to the
StraightAndNarrowPath, and your code will be without sin."

If you insist that your CodingConventions follow the rules of structured


programming, you reveal yourself as a martinet who sees rules as a way to crush
independence and creative thought, rather than as tools to improve productivity
and foster effective communication.

There is little advantage to "independence and creative thought" in the ordering


of program statements. It is much better to be independent and creative in the
solution of user problems.

Writing software should be treated as a creative activity. Just think about it: the
software that's interesting to make is software that hasn't been made before.
Most other engineering disciplines are about building things that have been built
before. -- RichardGabriel, quoted from
http://java.sun.com/features/2002/11/gabriel_qa.html

See SoftwareEngineeringIsCreative, ProceduralMethodologies.

Also of interest: GoTo, GoToProblem, GotoConsideredHarmful

CategoryCodingConventions CategoryCodingIssues CategoryJargon


ProgrammingParadigm

Bottom of Form

EditText of this page (last edited May 10, 2010)


FindPage by searching (or browse LikePages or take a VisualTour)

Vous aimerez peut-être aussi