0% found this document useful (0 votes)
11 views

CIT 302 COMPUTER PROGRAMMING

The document discusses structured programming, emphasizing its hierarchical organization of modules with single entry and exit points, which enhances programmer productivity and program clarity. It outlines three main structures: sequential, conditional branch, and iteration, along with pseudocode examples for selection structures. Additionally, it introduces defensive programming principles, advocating for input validation and simplicity to minimize errors.

Uploaded by

osarienjoseph
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

CIT 302 COMPUTER PROGRAMMING

The document discusses structured programming, emphasizing its hierarchical organization of modules with single entry and exit points, which enhances programmer productivity and program clarity. It outlines three main structures: sequential, conditional branch, and iteration, along with pseudocode examples for selection structures. Additionally, it introduces defensive programming principles, advocating for input validation and simplicity to minimize errors.

Uploaded by

osarienjoseph
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CIT 302 COMPUTER PROGRAMMING

3.0 Concepts of structured, modular and defensive programming


Structured Programming is a technique for organizing programs in a hierarchy of modulus.
Each module has a single entry and a single exit.

Structured programming design is a programming tool developed in the 1960s as a way of


defining the elements of a problem and as at today it's the best approach to all computer
programming tasks.

Structured programming gives room for well-thought-out program logic and provides an
attempt to keep programs as simple and straight forward as possible. Also structured
programming
i. increases programmer productivity
ii. increases program clarity by reducing complexity
iii. reduces program testing time
iv. reduces program maintenance time and effort.

In structured programming, Control is passed downward through the structure without


unconditional branches (e.g., goto statements) to higher level of the structure. There are three
types of structures: sequential, conditional branch and iteration.

i. Sequence Structure: In sequential structure or what you can also call sequential logic,
the steps are executed one after another as serial operations. This can be illustrated by the
following flowchart:

Process A

Process B

Process C

Looking at the above flowchart, there are two arrows, one at the at the bottom the last process
box. They respectively represent the ENTRY and EXIT points of the program segment. It is
good to state basic guideline of structured programming is that each module should have only
one entry point and one exit point. For this reason, a program that has only one entry and one
exit is called a proper program.

ii. Selection Structure:


The selection structure uses conditions and based on decision taken by the computer after
comparison of data, one of the available alternatives is selected. For this reason, it is also called
Conditional structure. It is also known as the IF structure. The selection structures can be
categorized into
i. Single Alternation
ii. Double Alternation

1
i. Multiple Alternation.

Condition True

Process

False

In the above flowchart, if the condition is satisfied, then the instruction(s) in the Process box
(or what is also called ACTION BLOCK) is/are executed. If the condition is not satisfied, the
control transfers to the next instruction following the checking of the condition.

The pseucode is as follows:


If Condition THEN
Action-block statement(s)
END IF

Double Alternation:
The structure is illustrated below:

False True
Condition

ELSE THEN
process process

Exercise 1:
Construct the pseudocode for the above structure.

Answer
IF condition THEN
Action — block – 1
ELSE
Action block — 2
ENDIF

2
Where action block 1 constitutes the THEN process and action-block-2 represents the ELSE
process.

Multiple Alternation:
The structure is seen in the following flowchart using three conditions:

True THEN
Condition - 1 PROCESS - 1

False

True THEN
Condition - 2
PROCESS - 2

False

True THEN
Condition - 3
PROCESS - 3

False

THEN
Condition - 4
PROCESS - 4

False

ELSE Process

Looking at the above flowchart, you will see that if none of the four conditions is satisfied,
then the process in the ELSE box will be executed.

Basically, structured programming employs Modular Approach and Top-Down Principle.


Modular Programming where a program is divided into parts or modules for easy development
and maintenance. A module is a collection of variables and functions that are grouped together

3
in a single file. The variables and functions in a module are usually related to one another in
some way. Examples of modular programming languages are object-oriented programming
languages like Java, C++, etc.

While defensive programming is an approach in which the programming assumes that there
may be undetected faults or inconsistencies in a code. In defensive programming, it is
recommended that plausibility checks must be carried out (defensive programming). The
methods for defensive programming include data control, (type, field limits, plausibility checks
etc).

Rules of defensive programming


1. Never assume anything. All input must be validated against a set of all legal inputs.
Then determine the action you take if the input is correct.

2. Use coding standards

3. keep your code as simply as possible because complexity breeds bugs. Functions should
be viewed as a contract between the user and the coder. The coder guarantees that the function
will execute only a specific task and if it cannot do that task (divide by zero), it can notify the
calling function that error has occurred.

4.0 Object Orientation re-visited, Java IDE (Creating, Saving and Opening of
Program Files)

You might also like