Académique Documents
Professionnel Documents
Culture Documents
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)
Bottom of Form
structured programming
Mid-market
CIO
structured
programmin
Show me everything on CIO-Midmarket Resources
g Research
and Reports
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
counter = 0;
while (counter < 5)
{
cout << "\nI love computers!";
counter ++;
}
Definitions
Definition 1: control structures
Definition 2: sequence
Definition 3: selection
Definition 4: iteration
Definition 5: branching
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:
"...it just seems like good programming practice to me." -- ChuckMoore, on one
of Dijkstra's papers on StructuredProgramming
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
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.
Multiple entry points can be done in any language that supports labels and GoTo
commands; this includes a lot more languages than just AssemblyLanguage.
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
Bottom of Form