Structured Programming Part 1

What is Structured Programming?

Over the years structured programming has come to mean different things to different people. It could refer to coding style, variable naming convention, modular programming, use of custom libraries and header files, object oriented programming, or how to effectively make use of data structures. To me, structured programming is none of the above.

Structured Programming is a Top-Down Design methodology that utilizes graphical control structures in order to create a visual map of program flow. This comes ahead of coding. Flow-charting is at the essence of Structured Programming.

The origins of structured programming can be attributed to work by computer scientists Edsger Dijkstra, Tony Hoare, et al, back in the early 1960s. The objective at the time was to make mammoth coding projects more manageable and successful. At the forefront of the original debate were attempts to eliminate "spaghetti code" by prohibiting the use of the GOTO statement.

At the heart of structured programming are some basic tenets:
1. Each control structure must have one and only one entry point.
2. Each control structure must have one and only one exit point.
3. The GOTO statement is to be eliminated.
4. Each program module must be no longer than a single printed page.

Here are the elements or control structures of Structured Programming.

Sequence
Every program or subprogram can be organised as a simple linear sequence, or a combination of linear sequences.

Decision

IF-THEN-ELSE Control Structure

A decision point can be evaluated to give a boolean result, TRUE or FALSE. There are only two paths to be taken.

Iteration
Iteration or looping can be implemented with different control structures:

In the WHILE-DO structure, the test is made before entering the loop.
In the DO-WHILE structure, one iteration of the loop (DO) is executed followed by the WHILE test for loop continuation.
REPEAT-UNTIL is a variation of the DO-WHILE, the difference being that the boolean result of the test has the opposite effect.

FOR-NEXT control structure is a variation of WHILE-DO.

Selection
SWITCH-CASE Control Structure

This is actually not a new structure but an extension of the IF-THEN-ELSE control structure. Because of its utility function it is sometimes considered as a fourth structure.

Complex Control Structures
More complex algorithms can be implemented by nesting any combinations of the four basic structures shown above. That is, any control structure is a SEQUENCE structure in itself and can be inserted as another SEQUENCE (i.e. a rectangular box). In other words, every rectangular box may be expanded into one of the four structures. Furthermore, all algorithms can be implemented entirely using these structures. The fundamentals remain the same.

MAIN
In the context of an embedded system, there is no purpose served by a STOP statement. Hence the lead-off flowchart may look like the following. This is usually implemented as a WHILE-DO structure where the WHILE test always produces a TRUE result.

Top-Down Design
Top-Down design subscribes to the adage "See the forest despite the trees". Top-Down design means seeing the big picture. Let's worry about the details later. Top-Down design embraces modularity, modular design and construction, and liberal use of functions and subroutines.

With high level functionality at its core, you can have a running program from the get-go.
Here is a template for a working program written in the C language, based on the flowchart shown above.

Code:
void Initialize(void)
{
}

void GetEvents(void)
{
}

void DoEvents(void)
{
}

void main(void)
{
Initialize();
while (1)
{
GetEvents();
DoEvents();
}
}

(c) 2017 MrChips 2017.10.07
Djsarakar

Author
MrChips
Views
591
Last update