Structured programming
Structured
Programming is a technique for organizing and coding computer programs in which
a hierarchy of modules is used, each having a single entry and a single exit
point, and in which control is passed downward through the structure without
unconditional branches to higher levels of the structure.
Three types of
control flow are used in structured programming:
1.Sequential: uses the series of statements executed one after another in
sequence.
2. Selection :
Uses conditional statements (if/
if-else selection structures , switch
multiple
selection structures)
3, Iteration.: Uses looping structures.(while, do/while, for structures)
So in structured program flow follows a simple
hierarchical model that employs sequence, selection and looping constructs such as "for,"
"repeat," and "while." . Use of the "Go To" statement is
discouraged.
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 sub-module, 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.
Each control
structures in a module must have single entry point and single exit point.
Connecting the control structures in sequence in which the exit point of one
control structures is directly connected to the entry point of the another
control structure, i.e, control structures are simply placed one after another
in a program. We can say this the control structure stacking.The rules for
forming structured program also allows the control structure nesting.
Rules for Forming Structured Programs
1.
Begin
with the “Simplest Flowchart “
2.
Any
rectangle(action) can be replaced by two rectangles(actions) in sequence.
3.
Any
rectangle(action) can be replaced by any control structure(sequence, if,
if/else, switch, while, do-while, for)
4.
Rules
2 and 3 may be applied as often as we like
in any order.
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.
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.
Functions and Structured Programming
By using
functions in our C programs, we can practice structured programming, in which
individual program tasks are performed by independent sections of program code.
"Independent sections of program code" sounds just like part of the
definition of functions. Functions and structured programming are closely
related.
The
Advantages of Structured Programming
Why is structured programming so great?
There are two
important reasons:
It's easier to write a structured program, because complex
programming problems are broken into a
number of smaller, simpler tasks. Each task is
performed by a function in which code and variables are isolated from
the rest of the program. We can
progress more quickly by dealing with these relatively simple tasks one at a time.
It's easier to debug a structured
program. If our program has a bug
(something that causes it to work improperly), a structured design makes
it easy to isolate the problem to a
specific section of code (a specific
function).
A related advantage of structured programming is the time we can save.
If we write a function to perform a certain task in one program, we can quickly
and easily use it in another program that needs to execute the same task. Even
if the new program needs to accomplish a slightly different task, we'll often find
that modifying a function we created earlier is easier than writing a new one
from scratch.
Let us consider
how much we've used the two functions printf() and scanf() even though we
probably haven't seen the code they contain. If our functions have been created
to perform a single task, using them in other programs is much easier.
Planning
a Structured Program
If we're going to
write a structured program, we need to do some planning first. This planning
should take place before we write a single line of code, and it usually can be
done with nothing more than pencil and paper. Our plan should be a list of the
specific tasks our program performs. Begin with a global idea of the program's
function. For example: if we were planning a program to manage our name and
address list then ,
Here are some obvious things:
Enter new names and addresses.
Modify existing entries.
Sort entries by last name.
Print mailing labels.
With this list, we've
divided the program into four main tasks, each of which can be assigned to a
function. Now we can go a step further, dividing these tasks into subtasks. For
example, the "Enter new names and addresses" task can be subdivided
into these subtasks:
Read
the existing address list from disk.
Prompt the user for one or more new entries.
Add the new data to the list.
Save the updated list to disk.
Likewise, the "Modify existing
entries" task can be subdivided as follows:
Read the existing address list from disk.
Modify one or more entries.
Save the updated list to disk.
We might have noticed that these
two lists have two subtasks in common--the ones dealing with reading from and
saving to disk. You can write one function to "Read the existing address
list from disk," and that function can be called by both the "Enter
new names and addresses" function and the "Modify existing
entries" function. The same is true for "Save the updated list to
disk."
By carefully dividing the program into tasks, we
can identify parts of the program that share common tasks. we can write
"double-duty" disk access functions, saving ourself time and making our
program smaller and more efficient. This method of programming results in a
hierarchical, or layered, program structure.
When we follow
this planned approach, you quickly make a list of discrete tasks that our
program needs to perform. Then we can tackle the tasks one at a time, giving
all our attention to one relatively simple task. When that function is written
and working properly, we can move on to the next task. Before we know it, our
program starts to take shape.
Note:
·
DO
plan before starting to code. By determining your program's structure
ahead of time, you can save time
writing the code and debugging it.
·
DON 'T try to do
everything in one function. A single function should perform a
single task, such as reading information from
a file.
No comments:
Post a Comment