Structured Programming
Learn basics of structured programming.
Structured Programming is a method of writing a computer program which uses; top-down analysis for problem solving; Modularisation for program structure and organisation; Structured code for the individual modules.
In a high level programming language such as Python and C#, algorithms are implemented via the following structure:
Sequence
Selection
Iteration
Sequence
A sequence is when a completed action triggers next action in a pre-determined order. Here's an example in Python:
Selection
Selection is when certain parts of code only executes once a condition is met, and therefore the flow of a program can change based upon the result of a condition. This is usually done via if-else-then statements. Here's an example in Python:
Many programming languages have SWITCH CASE statements which can be better than if-else-then statements if you have a lot of conditions. Here's an example in python:
Sometimes selection conditions may become very complex due to the amount of conditions combined into one statement via logical operators as well as nesting.
Iteration
Iteration mean repetition. Its when one or more instructions can be repeated without the need to re-write the code. Most high level languages have three types of iterative structures, which are:
WHILE LOOP: The condition is tested before entering the loop.
FOR LOOP: The loop is repeated for a specific number of times that is pre-specified.
REPEAT-UNTIL-LOOP: The condition is checked at the end of the loop to decide whether to continue or not.
Top-Down design model
Structured Programming uses a top-down design model. This means that the program is divided into modules which are called and used by the main program.
Any of the modules may be broken down into smaller sub-tasks, with the smallest modules performing a single function.
Hierarchy Charts
Hierarchy charts are used to show overall structure of a program. Any module this is shown below another module is part of the module above. Execution takes place from left to right, always at the lowest level component. A disadvantage of hierarchy charts is that selection and iteration can not by shown by them.
Advantages of Structured Programming
The main advantage of structured programming is that programs can be written easily and quickly and it makes it easy for others to understand them whilst reading the code. Here are a few more advantages:
Easier to program and manage as everything is broken into sub-parts.
Modules can be tested individually.
Same modules can be used multiple times throughout the program.
Frequently used modules can be saved onto a library and used by other programs.
Different programmers can work on different modules simultaneously, thus shortening development time.
It is easier to find errors in small self-contained modules.
New features can be added by adding new modules.
Easier to find which module needs updating.
Good Programming Practices
Here are a few tips for efficient programming:
Use meaningful variable/subroutine names.
Add comments to explain complicated functions.
Each module should perform a single task
Selection and Iteration should have single entry/exit points.
Use local variables to keep modules self contained.
Modular Programming
Modular design and programming techniques are most useful for large, complex programs. These types of programs have thousands of lines of code. In small programs that are less than a page long, it may not even be worth writing individual modules for every task.
Last updated