Sie sind auf Seite 1von 31

STRUCTURED PROGRAMMING

KASNEB CICT PAPER NO.9


8.0 LEARNING OUTCOMES
A candidate who passes this paper should be able to:
• Analyse a problem and design an appropriate solution
• Write codes using C programming language
• Test and debug a structured program code
• Produce documentation, both user and technical, to support programs.
CONTENT
8.1 Introduction to structured programming
• Introduction to programming languages
• Types of programming languages
• Generations of programming languages
• Programming approaches
• Language translators
• Basic concepts of structured programming
• Problem definition, structure and design
• Integrated development environment (IDE)
8.2 Programming basics
• Variables and data types
• Input output statements
• Assignments
• Namespaces
• Comments
• Pre-processor directives
• Expressions and operators
• Control structures
• Writing and running a simple program
8.3 Functions/sub-programs
• Functions verses procedures
• Parameter passing
• Recursion
• Calling procedures
• Argument naming
• Event procedures
• Testing and debugging errors
• Writing and running a program using functions and procedures
8.4 Data structures
• Arrays
• Pointers
• Linked lists
• Unions
• Writing a program using data structures
2 Structured Programming

8.5 File handling (Input/Output)


• Opening files
• Writing to files
• Closing files

8.6 Application development


• Mobile application development
• Collaborative application development
8.7 Documentation
• User manuals
• Technical manuals
8.8 Emerging issues and t rends
3 Structured Programming

STRUCTURED PROGRAMMING

Introduction
8.11 Definition and classification of programming languages

Structured programing is a programming paradigm aimed on improving the clarity, quality,


and development time of a computer program by making extensive use
of subroutines, block structures and while loops—in contrast to using simple tests and
jumps such as the goto statement which could lead to "spaghetti code" which is both
difficult to follow and to maintain.

It emerged in the 1960s—particularly from work by Böhm and Jacopini, and a famous
letter, Go To Statement Considered Harmful, from Edsger Dijkstra in 1968—and was
bolstered theoretically by the structured program theorem, and practically by the
emergence of languages such as ALGOL with suitably rich control structures.

Programming Language Classifications

There is no exact system for the classification of programming languages. Usually,


classification is determined by programming paradigm. Another mode of classification is by
the intended domain of use.

A language is a medium for communication. The languages we speak are called natural
languages. A programming language is a subset of the set of natural languages. It contains
all the symbols, characters, and usage rules that permit a human being to communicate
with computers. A variety of programming languages have been invented over the years of
computer history. However, every programming language must accept certain types of
written instructions that enable a computer system to perform a number of familiar
operations. In other words, every programming language must have instructions that fall
under the following categories:

• Input/output Instructions: A program needs input data from the external world (or
sometimes given implicitly) with which it performs operations on the input data, and
generates output (compare with algorithm). Input/output instructions. Provide
details on the type of input or output operations to be performed, and the storage
locations to be used during the operations, hence they are provided with purpose.
• Arithmetic Instructions: A program might be required to perform arithmetic
operations on the data in the program. Arithmetic instructions are provided for the
requirement. These perform the arithmetic operations of addition, subtraction,
multiplication, division, etc.
• Logical/Comparison Instructions: They are used to compare two values to check
whether the values satisfy a given condition or state.
• Storage/Retrieval and Movement Instructions: These are used to store, retrieve, and
move data during processing. Data may be copied from one storage location to
another and retrieved as required.
4 Structured Programming

• Control Instructions: These are selection and loop constructs which aid in out-of-
sequence program flow.

Although all programming languages have an instruction set that permits these familiar
operations to be performed, a marked difference is found between the symbols and syntax
used in machine languages, assembly languages, and high-level languages.

1.8.1. Assembly Language

Some applications are developed by coding in assembly language – a language closest to the
machine language. This type of application software is most efficient for processing data.
However, since a particular flavor of the assembly language is designed for a particular
architecture, the type of assembly language understood by a computer depends upon the
underlying architecture of the microprocessor used. For example, if a Zilog® microprocessor
is used, then the machine language understood is Z-80. On the other hand, a flavour of the
Intel® assembly language is used for a Pentium® microprocessor which differs from the Z-80.
Thus, assembly languages are architecture-dependent.

1.8.2. High-level Languages

Given the inability of assembly language to adapt across different architectural platforms
and given the varied nature of real-world problems, a variety of computer languages, called
high-level languages, were developed, each suited best to a model in a particular class of
problems. High-level language programs are architecture-independent, easier to write, and
provide easier maintenance and readability than their assembly-language counterparts.
However, they require longer execution time compared to assembly-language programs.

Different high-level programming languages were introduced in the 1950s to reduce the
problems that arose in writing code in assembly and machine language. When the first high-
level languages were developed, the longer translation and execution time were considered
as serious limitations of the technique. Nonetheless, the following factors contributed to the
popularity of high-level languages for application development:

• Savings in Programming Time and Training: High-level languages were easier to learn
and understand. Thus, it took less time and effort to write an error-free program or
to make corrections and code-revisions.
• Increased Speed and Capacity of Hardware: The third and fourth generations of
computer hardware brought about a revolution in access time, memory and disk
capacities. This caused the time overhead incurred by the use of high-level languages
to be tolerable.
• Increasing Complexities of Software: With time, software systems grew more
complex. This necessitated use of complex constructs each of which could represent
several lines of assembly code and was provided by high-level languages.

Some popular high-level languages include FORTRAN (FORmula TRANslation), COBOL


(COmmon Business Oriented Language), Pascal (after Blaise Pascal), C, and Ada (after Ada
Byron, Countess of Lovelace – the world’s first computer programmer).
5 Structured Programming

Additional note

Computer programming language can be classified into two major categories:

•Low Level
•High Level

Low Level Languages

The languages which use only primitive operations of the computer are known as low
language. In these languages, programs are written by means of the memory and registers
available on the computer. As we all know that the architecture of computer differs from
one machine to another, so far each type of computer there is a separate low level
programming language. In the other words, Programs written in one low level language of
one, architectural can’t be ported on any other machine dependent languages. Examples
are Machine Language and Assembly Language.

Machine Language

In machine language program, the computation is based on binary numbers. All the
instructions including operations, registers, data and memory locations are given in there
binary equivalent.

The machine directly understands this language by virtue of its circuitry design so these
programs are directly executable on the computer without any translations. This makes the
program execution very fast. Machine languages are also known as first generation
languages.

A typical low level instruction consists essentially of two parts:

•An Operation Part:


Specifies operation to be performed by the computer, also known as Opcode.

•An Address Part:


Specifies location of the data on which operation is to be performed.

Advantages

Machine language makes most efficient use of computer system resources like storage,
registers, etc. the instruction of a machine language program are directly executable so
there is no need of translators. Machine language instruction can be used to manipulate the
individual bits in a computer system with high execution speed due to direct manipulation
of memory and registers.

Drawbacks

FOR FULLNOTES
Machine languages are machine dependent and, therefore, programs are not portable from

CALL/TEXT:0724 962 477


one computer to other. Programming in machine language usually results in poor
6 Structured Programming

programmer productivity. Machine languages require programmers to control the use of


each register in the computer’s Arithmetic Logic Unit and computer storage locations must
be addressed directly, not symbolically. Machine language requires a high level of
programming skill which increases programmer training costs. Programs written in machine
language are more error prone and difficult to debug because it is very difficult to
remember all binary equivalent of register, opcode, memory location, etc. program size is
comparatively very big due to non-use of reusable codes and use of very basic operations to
do a complex computation.

Assembly Language

Assembly language are also known as second generation languages. These languages
substitutes alphabetic or numeric symbols for the binary codes of machine language. That is,
we can use mnemonics for all opcodes, registers and for the memory locations which
provide us with a facility to write reusable code in the form of macros. Has two parts, one is
macro name and the other is macro body which contains the line of instructions. A macro
can be called at any point of the program by its name to use the instruction. A macro can be
called at any point of the program by its name to use the instructions given in the macro
repetitively.

These language require a translator known as “Assembler” for translating the program code
written in assembly language to machine language. Because computer can interpret only
the machine code instruction, once the translation is completed the program can be
executed.

Advantages

Assembly languages provide optimal use of computer resources like registers and memory
because of direct use of these resources within the programs. Assembly language is easier
to use than machine language because there is no need to remember or calculate the binary
equivalents for opcode and registers. An assembler is useful for detecting programming
errors. Assembly language encourages modular programming which provides the facility of
reusable code, using macro.

Drawbacks

Assembly language programs are not directly executable due to the need of translation.
Also, these languages are machine dependent and, therefore, not portable from one
machine to another. Programming in assembly language requires a high level of
programming skills and knowledge of computer architecture of the particular machine.

High Level Languages (HLL)

All high level language are procedure-oriented language and are intended to be machine
independent. Programs are written in statements akin to English language, a great
advantage over mnemonics of assembly languages require languages use mnemonics of
assembly language. That is, the high level languages use natural language like structures.
These languages require translators (compilers and interpreters) for execution. The
7 Structured Programming

programs written in a high level language can be ported on any computer that is why they
are known as machine independent. The early high-level language comes in third generation
of languages, COBOL, BASIC, APL, etc.
These languages enable the programmer to write instruction using English words and
familiar mathematical symbols which makes it easier than technical details of the computer.
It makes the programs more readable too.

Procedures

Procedures are the reusable code which can be called at any point of the program. Each
procedure is defined by a name and set of instructions accomplishing a particular task. The
procedure can be called by its name with the list of required parameters which should pass
to that procedure.

Advantages of High Level Languages

These are the third generation languages. These are procedure-oriented languages and are
machine independent. Programs are written in English like statements. As high level
languages are not directly executable, translators (compilers and interpreters) are used to
convert them in machine language equivalent.

Advantages

1) These are easier to learn than assembly language.


2) Less time is required to write programs.
3) These provide better documentation.
4) These are easier to maintain.
5) These have an extensive vocabulary.

Limitation of Programming language

1) A long sequence statements is to be written for every program.


2) Additional memory space is required for storing compiler or interpreter.
3) Execution time is very high as the HLL programs are not directly executable.

8.12 Basic concepts of structured programming

Basic Programming Concepts

Even though each programming language you use is unique, there are certain concepts
common to all languages, including Live Code’s scripting language. Let's look at three of the
most common concepts and structures used in programming.

I. Sequence of commands (The right commands in the right order.)

FOR FULLNOTES
It is important not only to give the right commands or steps—they must also be
given in the correct sequence. We can easily see in some of our mundane

CALL/TEXT:0724 962 477


examples—making a sandwich, tying one's shoes, following a recipe—that proper
8 Structured Programming

order is essential to our success. We might call such obvious sequences task order,
because the proper sequence is dictated by the nature of the task.

But there are also procedures in which the order of steps is unimportant. Often in
such procedures, a conventional order emerges to avoid confusion. An excellent
example in the U.S. context is addressing letters for mailing. Every school child
knows that you do it in this order:

First name Last name


House Number Street name
City, State Zip code
Country

Oddly enough, this conventional order is exactly the reverse of how the address is
examined at the various postal distribution points in the mail system. The post office
at the point of origin would look at the country and put the letter in a pile for
international mail. The distribution center in the destination country would look at
the zip code and perhaps state. The local post office would examine the street
address and place it in the proper mail carrier's route, and the mail carrier, upon
arriving on that street, would place it into the mailbox bearing that address. Finally,
whoever checked the mail at the house would look at the name and gives the letter
to the person it was addressed to.

In some countries the conventional order follows the logical task order in addressing
envelopes. In Russia, for example, letters are addressed in exactly the opposite order
to the U.S. convention.

Example: You want to clear your screen of all buttons and fields, show a field with
text, and wait for the user to click, then hide the field and show the former ones.
To work correctly, not only do all the commands have to be there, they have to be in
the right order.
9 Structured Programming

II. Conditional structures (Do certain things based on a true or false, yes or no
decision.)
These provide for one outcome or sequence of events to be executed if a statement
is true, and another outcome or sequence of events to be triggered if the statement
is false.

In most programming languages these structures take the form if . . . then . . . else.

The One Potato, Two Potato game uses lots of conditional decisions.
If the counter lands on your fist on the word "more" then you must remove your fist
from the circle.
If both of your fists are knocked out of the circle then you are out of the game.

Computing examples:

Example 1:
If a word exists in a list, then print it out,
Else tell the user that the word does not exist.

Example 2:
If a sentence contains the word "silly" then put that sentence into the silly list.
Else if it doesn't contain the word "silly" then put it into the serious list.

III. Looping structures (A list of instructions to do more than once.)


Used to make the computer repeat a certain command or sequence of commands.
The loop may run for a predetermined number of times, until a certain condition
becomes true, or as long as a certain condition remains true.

Here are some ways that looping might be done:


Do the following 20 times.
Do the following once for each word in the list.
Repeat the following until the user presses the option key.
Repeat the following as long as the option key is depressed.

Again, the One Potato game provides an obvious example of a looping structure. The
rhyme is repeated and fists counted for as many times as needed until just one
person is left.

Another Example:
Given a list of party guests, assign everyone to one of three groups for "ice-breaker"
games.

FOR FULLNOTES
Programming Strategies

CALL/TEXT:0724 962 477


10 Structured Programming

Programming can range in complexity from solving small problems—like setting an alarm
time on your watch or cell phone—to very sophisticated instructional or business
applications. For more complex tasks, you can use these strategies to help you think through
the logic of your program before starting to write code.

Top-down design

Top-down design is a way of approaching a complex programming task by first mapping out
the entire program and identifying the major components that it will require. Then the
programmer would use flowcharts and general statements to represent the logical flow of
your program. Once the major components are identified, the programmer then focuses on
each component in greater detail, finally culminating in writing the actual program code for
creating each component.

In the example at right, we have represented the “program” for playing One Potato, Two
Potato using a top-down approach. Each shape in the flowchart represents a major step in
the game. Combinations of shapes and arrows show conditional “if-then” decision points, as
well as looping structures in which segments of the program are repeated, perhaps with
slight variations in each iteration.
11 Structured Programming

Pseudo code
This term, from the prefix pseudo-, 'false' and the root word code, 'programming
instructions', describes a way of representing the detailed steps your program must perform
without having to worry about the specific vocabulary or syntax of a specific programming
language. You use your knowledge of the basic control structures, common sense and logic
to write plain-English statements to explain in detail how you will accomplish each main
step. All of the examples shown here and used in class could be considered forms of pseudo
code.

Here is an example of pseudo code that describes our One Potato, Two Potato “program”.
Notice how it attempts to detail all of the steps, conditional statements and looping
segments using simple statements, indents and minimal punctuation brackets.

8.13 Problem definition, Structure and Design

PROBLEM SOLVING & PROGRAM DESIGN


Two phases involved in the design of any program:
(i) Problem Solving Phase
(ii) Implementation Phase
1. In the problem solving phase the following steps are carried out:
• Define the problem
• Outline the solution
• Develop the outline into an algorithm
• Test the algorithm for correctness
2. The implementation phase comprises the following steps:
FOR FULLNOTES
• Code the algorithm using a specific programming language
• Run the program on the computer
CALL/TEXT:0724 962 477
12 Structured Programming

• Document and maintain the program


• Structured Programming Concept
Structured programming techniques assist the programmer in writing effective error free
programs.
The elements of structured programming include:
-Top-down development
-Modular design.
-The Structure Theorem
It is possible to write any computer program by using only three (3) basic control structures,
namely:
-Sequence Selection (if then-else)
-Repetition (iteration, looping, Do While)
-algorithms
ALGORITHMS
An algorithm is a sequence of precise instructions for solving a problem in a finite amount of
time.
Properties of an Algorithm:
• It must be precise and unambiguous
• It must give the correct solution in all cases
• It must eventually end.
Algorithms and Humans
Algorithms are not a natural way of stating a problem’s solution, because we do not
normally state our plan of action.
-We tend to execute as we think about the problem. Hence, there are inherent difficulties
when writing an algorithm.
-We normally tailor our plans of action to the particular problem at hand and not to a
general problem (i.e. a near sighted approach to problem solving)
-We usually do not write out our plan, because we are usually unaware of the basic ideas
-We use to formulate the plan. We hardly think about it -we just do it.
Computer programmers need to adopt a scientific approach to problem solving, i.e. writing
algorithms that are comprehensive and precise.
We need to be aware of the assumptions we make and of the initial conditions.
Be careful not to overlook a step-in procedure just because it seems obvious.
Remember, machines do not have judgment, intuition or common sense!
Developing an Algorithm
Understand the problem (Do problem by hand. Note the steps)
Devise a plan (look for familiarity and patterns)
Carry out the plan (trace)
Review the plan (refinement)
Understanding the Algorithm
13 Structured Programming

Possibly the simplest and easiest method to understand the steps in an algorithm, is by
using the flowchart method. This algorithm is composed of block symbols to represent each
step in the solution process as well as the directed paths of each step.
Find the average of a given set of numbers.
Solution Steps
1. Understanding the problem:
(i) Write down some numbers on paper and find the average manually, noting each step
carefully .e.g. given a list say: 5, 3, 25, 0, 9
(ii) Count numbers | i.e. How many? 5
(iii) Add them up | i.e. 5 + 3 + 25 + 0 + 9 = 42
(iv) Divide result by numbers counted | i.e. 42/5 = 8.4
2. Devising a plan:
Make note of not what you did in steps (i) through (iv), but how you did it. In doing so, you
will begin to develop the algorithm.
How do we count the numbers?
Starting at 0 i.e. set COUNTER to 0
Look at 1st number, add 1 to COUNTER
Look at 2nd number; add 1 to COUNTER and so on, until you reach the end of the list
How do we add numbers?
Let SUM be the sum of numbers in list.
Set SUM to 0.
Look at 1st number, add number to SUM
Look at 2nd number, add number to SUM and so on, until we reach end of list
How do we compute the average?
Let AVG be the average then AVG= total sum of items i.e. SUM number of items COUNTER
3. Identifying patterns, repetitions and familiar tasks.
Familiarity: Unknown number of items? i.e. n item Patterns :look at each number in the list
Repetitions: Look at a number Add number to sum Add 1 to counter
4. Carrying out the plan. Check each step Consider special cases, Check result, Check
boundary conditions: i.e. what if the list is empty?
Division by 0?
Are all numbers within the specified range?
In this example, no range is specified
-No special cases.
Check result by tracing the algorithm with a list of numbers e.g. 7, 12, 1, 5, 13.
If list is empty, we do not want to compute average.
A Systematic Approach to Defining a Problem
Defining the problem is the first step towards a problem solution. A systematic approach to
problem definition, leads to a good understanding of the problem. Here is a tried and tested
FOR FULLNOTES
method for defining (or specifying) any given problem:
Divide the problem into three (3) separate components:
CALL/TEXT:0724 962 477
14 Structured Programming

(a) Input or source data provided


(b) Output or end result required
(c) Processing
-a list of what actions are to be performed
EXAMPLE 1:
A program is required to read three (3) numbers add them and print their total.
It is usually helpful to write down the three (3) components in a defining diagram as shown
below:
INPUT PROCESSING OUTPUT
Read: Num1, Num2, Num3
Total = Num1 + Num2 + Num3
Print: Total
EXAMPLE 2:
Design a program to read in the max. and min. temperature on a particular day and
calculate and print the average temp.
INPUT PROCESSING OUTPUT
Read: max_ temp, min_ temp, Calculate avg_temp
Print: avg_temp
Once the problem has been properly defined, making sure that no assumptions are made
and every action required is stated in the Processing column, we can proceed to develop the
algorithm. To do this, simply focus on the actions in the processing column, then write down
the steps that specify how to perform each action and in what sequence.
Sample solution for EXAMPLE 1
Algorithm Add _Numbers
Read Num1, Num2, Num3
Total = Num1 + Num2 + Num3
Print Total
Stop
Top-Down Design Approach (Modularization)
The modularization approach involves breaking a problem into a set of sub-problems,
followed by breaking each sub-problem into a set of tasks, then breaking each task into a set
of actions.
Problem 1
Add 23 and 35-no further refinement is required.
Problem 2
Turn on a light bulb Sub-problem 1: locate bulb (one task, one action)
Sub-problem 2: depress switch
Problem3: Given a list of students’ test scores, find the highest and lowest score and the
average score.
Sub-problem 1: read students ‘scores
Sub-problem 2: find highest score
15 Structured Programming

Sub-problem 3: find lowest score


Sub-problem 1 can be considered as one action and therefore needs no further refinement.
Sub-problems2 and 3 however can be further divided into a group of actions.
Advantages of the Top-Down Design Method
•It is easier to comprehend the solution of a smaller and less complicated problem than to
grasp the solution of a large and complex problem.
•It is easier to test segments of solutions, rather than the entire solution at once. This
method allows one to test the solution of each sub-problem separately until the entire
solution has been tested.
•It is often possible to simplify the logical steps of each sub-problem, so that when taken as
a whole, the entire solution has less complex logic and hence easier to develop.
•A simplified solution takes less time to develop and will be more readable.

The program will be easier to maintain.

Structured program designs

Purpose

This discusses the basic principles that underlie structured program design and functional
decomposition. The objective of functional decomposition is to design structured programs
that are easy to test, debug, and maintain. The basic idea is to break down (or decompose) a
program into logically independent modules based on the processes or tasks they perform.

Strengths, weaknesses, and limitations

Because the detailed computational logic is grouped into independent, single function
modules, well-structured programs are easier to test, debug, and maintain than are
unstructured programs. Independent modules can be independently coded and tested. The
control structure allows the entire program to be tested top down, one module at a time.
When an error occurs, it is often possible to quickly isolate the likely cause to a single
module. During the maintenance phase, independent modules can be replaced or modified
with minimal ripple effects.

Functional decomposition is a process driven methodology. Consequently, although the


logic modules are independent, the data are not, and that can lead to ripple effects during
testing, debugging, and maintenance.

Inputs and related ideas

Before starting to design a program using functional decomposition, the necessary logical
data structures and the primary processes must be defined during the systems analysis
FOR FULLNOTES
phase (Part IV) of the system development life cycle. Often, the results of analysis are
documented in the requirements specification .Additionally; it is useful to define the

CALL/TEXT:0724 962 477


16 Structured Programming

physical data structures, file structures, and database structures. The required input and
output data structures, and the algorithms before starting program design.

The design of a structured program is sometimes documented using the HIPO technique.
Structure charts are useful planning tools. Such tools as logic flowcharts Nassi- Shneiderman
charts, decision trees, decision tables, pseudo code, structured English, and IPO charts are
used to document the modules.

4 Concepts

Functional decomposition is a popular structured program design methodology. The basic


idea is to break down (or decompose) a program into logically independent modules based
on the processes or tasks they perform.

4.1 The control structure

A well-designed structured program consists of a set of independent, single function


modules linked by a control structure that resembles a military chain of command or an
organization chart (Figure1). Each module is represented by a rectangle. At the top of the
control structure is a single module called the root (or the main control module). All control
flows from the root which calls (or invokes) its level-2 child (or son) modules. The level-2
modules, in turn, call their level-3 children, and so on. The calling module (sometimes called
the parent) passes data and/or control information to the child and receives data and/or
control information back from the child; otherwise, the modules are viewed as independent
black boxes. Note that control always returns to the calling module.

Figure 1 A well-designed structured program consists of a set of independent single function


modules linked by a control structure.
17 Structured Programming

A module with no children (a lowest-level module) is called a leaf and often implements a
single algorithm. Library modules (e.g., a standard subroutine) are indicated by a rectangle
marked with two vertical lines; see the leaf labeled Library module in Figure1. Note that a
library module can be called by more than one parent.

The modules are often assigned identifying numbers or codes that indicate their relative
positions in the hierarchy. For example, the root might be designated module 1.1, the level-
2 modules might be designated 2.1, 2.2, 2.3, and so on. Other designers use letters (or even
Roman numerals) to designate levels; for example, module A.1 is the root, module B.3 is the
third module at level 2, module C.6 is the sixth module at level 3, and so on. Sometimes,
more complex numbering schemes are used to indicate a path through the hierarchy. The
key is consistency.

4.2 Designing a control structure

The first step in decomposing a program is to define its high-level control structure. The
primary inputs come from the logical models developed during the systems analysis phase
(Part IV) and from the requirements specification. More specifically, the high-level functions
to be performed by a given program can often be obtained from the appropriate
configuration item’s process description.

4.2.1 Afferent, transform, and efferent processes

One approach to designing a control structure is to divide the functions (or sub processes)
into three groups (Figure 2). The afferent processes gather and prepare input data. The
efferent processes structure and transmit output data. In the middle, the transform
processes convert the input data to output form. Identifying the afferent, transform, and
efferent processes suggests a basic input, process, output control structure.

FOR FULLNOTES
CALL/TEXT:0724 962 477
18 Structured Programming

Figure 2 Afferent, transform, and efferent processes.

4.2.2 Trigger events

An alternative is to start with the program’s trigger event, the event that activates the
program or causes it to change from a wait to a run state. Some programs are triggered by
an asynchronous event such as the arrival of a transaction or an interrupt. Other
applications are clock driven; for example, a batch program might be run at the same time
every week, and a scientific data collection routine might take a sample every few seconds.
A program’s high-level control structure should reference those tasks that are performed in
direct response to its trigger event.
19 Structured Programming

4.2.3 Data structures

Another technique for designing a high-level control structure is to analyze the data
structures. The point of any program is to accept the input data and convert them to the
form required for output, so the data actually drive the program design process.

Analyze the output data and determine the order in which the various output substructures
are assembled. Then define the input substructures and the algorithms that generate the
data elements in a given output substructure. The data structures will, essentially, dictate
both the order in which the logical tasks must be performed and the logical structures
needed to support those tasks. Generally, sequential data structures call for sequential
logic, conditional structures call for conditional logic, and repetitive structures call for
repetitive logic.

4.2.4 Logical access maps

Logical access maps were initially proposed by Martin to help the designer determine the
logical execution sequences or access paths through a program. This technique recognizes
that each user (or class of users) has a unique logical accessing perspective. For example, a
sales associate might start with a customer order and follow the order through order
fulfillment, shipping, and so on. In contrast, warehouse personnel might start with the
arrival of a shipment from a supplier, track the shipment into inventory, and view a
customer order as nothing more than a transaction that deletes individual items from
inventory. The point of logical access mapping is to examine the logical accessing sequence
of a system’s programs and a program’s modules from multiple perspectives and to use the
consensus view as a design criterion.

4.3 Evaluating the control structure

A well-designed control structure exhibits a regular morphology (form, or structure) and


achieves a balance between breadth and depth.

4.3.1 Morphology

One way to evaluate a control structure’s design is to examine its morphology (form or
structure). Each module decomposes into several lower-level routines, so the number of
modules should increase from level-2 to level-3, perhaps increase again at level-4, and so
on. Eventually, however, only some modules require additional decomposition, so the
number of routines at each level begins to stabilize and then to decline.

For example, Figure 3 shows a control structure for an inventory management


program. Figure 4 is a simplified version of the control chart that emphasizes the number of
FOR FULLNOTES
modules at each level. Note the shape; some people describe it as a mosque or a cigar. Most

CALL/TEXT:0724 962 477


20 Structured Programming

good control structures have a similar shape, with the number of modules at each level
increasing, then stabilizing, and then decreasing.

Morphology is subjective; over the years, people have noticed that good designs tend to
have that characteristic shape. The designer should not consciously try to make the control
structure resemble a mosque. Instead, morphology should be checked after the design is
complete. If the shape seems reasonable, the design is probably a good one. If the design
deviates significantly from the expected shape, it should be restudied.

Figure 3 A control structure.


21 Structured Programming

Figure 4 The morphology of a good design resembles a cigar.

4.3.2 Depth and breadth

A well-designed control structure balances two conflicting objectives: depth and breadth.
Depth is the number of levels in the control structure. Because each call to a lower level is a
potential source of error, shallow structures tend to be better than deep structures.
Breadth, or span-of-control, is a measure of the number of modules directly controlled by a
higher-level routine. Too many subordinates add to complexity, so narrow structures tend
to be better than broad structures.

Narrow structures are usually deep, so reducing breadth tends to increase depth, and vice
versa. One rule-of-thumb for balancing these two parameters suggests that no module
should directly control more than seven subordinates. If a given routine has too many
subordinate modules, adding a secondary control structure drops some of them to a lower
level.

Module size is another useful screen; one page of source code is a common limit. If a
module’s logic exceeds roughly 50 lines of code, decompose it. If, on the other hand, the
logic in a subordinate routine can be merged into its parent without violating the single
page rule, merge it. Remember, however, that rules-of- thumb are not absolute. If breaking

FOR FULLNOTES
one means a better design, break it.

4.3.3 Structure clash and boundary clash


CALL/TEXT:0724 962 477
22 Structured Programming

Structure clash occurs when corresponding elements of two related data structures are
incompatible. For example, imagine that customer social security number is the key for an
invoice file and customer zip code is the key for a name and address file. Because the files
are (presumably) stored in different record sequences and accessed by different keys, it is
difficult to design a program to efficiently merge them.

A boundary clash occurs when the physical data structures are incompatible. For example, if
a program sets a nonstandard (9 × 12) page size and the printer is loaded with standard (8.5
× 11) paper, the resulting boundary clash produces poorly aligned output.

Structure clashes and boundary clashes lead to errors and inefficiencies. The program
designer should carefully evaluate both the logical and physical data structures and change
the program design, the data structures, or both to eliminate structure clash and border
clash.

4.4 Module design

A good module is cohesive and loosely coupled.

4.4.1 Cohesion

Cohesion is a measure of a module’s completeness. Every statement in the module should


relate to the same function, and all of that function’s logic should be in the same module.
When a module becomes large enough to decompose, each sub-module should perform a
cohesive sub-function.

The best form of cohesion is called functional cohesion. A functionally cohesive module
performs a single logical function, receives and returns no surplus data, and performs only
essential logical operations. Functional cohesion is the designer’s objective. A module is not
considered function-ally cohesive if it exhibits other forms of cohesion.

Coincidental cohesion is the weakest type. In a coincidentally cohesive module, there is little
or no logical justification for grouping the operations; the instructions are related almost by
chance. In a logically cohesive module, all the elements are related to the same logical
function; for example, all input operations or all data verification operations might be
grouped to form a module. The elements that form a temporally cohesive module are
related by time; for example, a setup module might hold all operations that must be
performed at setup time.

Procedural cohesion is an intermediate form of cohesion, halfway between coincidental


cohesion and functional cohesion. All the elements in a procedurally cohesive module are
associated with the same procedural unit, such as a loop or a decision structure.
Communicational cohesion groups elements that operate on the same set of input or
output data (more generally, on the same data structure). With sequential cohesion, the
modules form a chain of transformations, with the output from one module serving as input
to the next. The three types of cohesion described in this paragraph often result from
viewing the program as a flowchart.
23 Structured Programming

4.4.2 Coupling

Coupling is a measure of a module’s independence. Perfect independence is impossible


because each module must accept data from and return data to its calling routine. Because
global data errors can have difficult-to-trace ripple effects, a module should never change
the value of any global data element that is not explicitly passed to it. If that rule is
enforced, the list of parameters becomes a measure of how tightly the module is linked to
the rest of the program. Fewer parameters imply looser coupling.

With data coupling (or input-output coupling), only data move between the modules. Data
coupling is necessary if the modules are to communicate. Control coupling involves passing
control information (e.g., a switch setting) between the modules. Hybrid coupling is a
combination of data coupling and control coupling. For example, if module A modifies an
instruction in module B, the operation looks like data coupling to module A and control
coupling to module B. Whenever possible, control and hybrid coupling should be eliminated.

With common-environment coupling, two or more modules interact with a common data
environment, such as a shared communication region or a shared file. With content
coupling, some or all of the contents of one module are included in the other. This problem
often occurs when a module is given multiple entry points. Both common-environment and
content coupling can lead to severe ripple effects, and should be avoided.

Binding time, the time at which a module’s values and identifiers are fixed, is another factor
that influences coupling. A module can be fixed (rendered unchangeable) at coding time, at
compilation time, at load time, or at execution time. Generally, the later the binding time
the better the module.

4.4.3 Sequence, selection, and repetition

The modules that form a well-structured program are composed of three basic logical
building blocks or constructs: sequence, selection (or decision), and repetition (or iteration).
Go to or branch instructions are not permitted.

Sequence (Figure 5) implies that the logic is executed in simple sequence, one block after
another. Note that each block might represent one or more actual instructions.

Selection (or decision) logic provides alternate paths through the block depending on a run-
time condition. With IF-THEN-ELSE logic (Figure 6), if the condition is true the logic
associated with the THEN branch is executed and the ELSE block is skipped. If the condition
is false the ELSE logic is executed and the THEN logic is skipped. A case structure (Figure 7)
provides more than two logical paths through the block of logic based (usually) on the value
of a control variable.

FOR FULLNOTES
CALL/TEXT:0724 962 477
24 Structured Programming

Figure 62.6 Selections.

Figure 62.7 A case structure.

There are two basic patterns for showing repetitive logic: DO WHILE and DO UNTIL
(Figure 8). In a DO WHILE block, the test is performed first and the associated instructions
are performed only if (while) the test condition is true. In a DO UNTIL block, the associated
instructions are executed first and then the exit condition is tested.
25 Structured Programming

Figure 8 Repetition.

5 Key terms
Afferent process — A process that gathers and prepares input data.
Binding time — The time at which a module’s values and identifiers are fixed; for example,
coding time, compilation time, load time, or execution time.
Breadth (span-of-control) — A measure of the number of modules directly controlled by a
higher-level routine.
Case structure — A selection structure with multiple alternative paths; the path through the
structure is normally based on the value of a control variable.
Child (son) — An immediate lower-level module in a control structure. Control passes from
the parent to the child and then returns to the parent.
Cohesion — A measure of a module’s completeness.
Coincidental cohesion — The weakest type of cohesion in which the elements are related
almost by chance.
Common-environment coupling — A form of coupling in which two or more modules
interact with a common data environment, such as a shared communication region or a
shared file.
Communicational cohesion — A form of cohesion that groups elements that operate on the
same set of input or output data or on the same data structure.
Configuration item — A functional primitive; above the configuration level are the system’s
logical, composite elements. Below the configuration level are the system’s physical
FOR FULLNOTES
components, including the programs.

CALL/TEXT:0724 962 477


26 Structured Programming

Content coupling — A form of coupling in which some or all of the contents of one module
are included in the other.
Control coupling — A form of coupling in which control information (e.g., a switch setting) is
passed between the modules.
Control structure — A hierarchical model of the flow of control through a program. The
control structure resembles a military chain of command or an organization chart. At the
top is a main control module that calls secondary control structures. At the bottom are the
computational routines, each of which implements a single algorithm.
Coupling — A measure of a module’s independence; fewer parameters flowing into or out
from a module imply looser coupling.
Data coupling (input-output coupling) — A form of coupling in which only data move
between the modules.
Depth — The number of levels in the control structure.
Efferent process — A process that structures and/or transmits output data.
Function cohesion — The strongest type of cohesion in which a given module performs a
single logical function, receives and returns no surplus data, and performs only essential
logical operations.
Functional decomposition — A program design methodology in which the program is
broken down (or decomposed) into modules based on the processes or tasks they perform.
Hybrid coupling — A combination of data coupling and control coupling.
Leaf — A module in a control structure with no lower-level (child) modules.
Logical access map — A program design tool used to help the designer determine the logical
execution sequences or access paths through a program.
Logical cohesion A form of cohesion in which all the elements are related to the same
logical function.
Morphology — Form or structure.
Procedural cohesion — A type of cohesion in which all the elements in a module are
associated with the same procedural unit, such as a loop or a decision structure.
Repetition (iteration) — A block of logic that is executed repetitively as long as (while) an
initial condition holds or until a terminal condition occurs.
Root — The module at the top of a control structure from which all control flows.
Selection (decision) — A block of logic that provides alternate paths through the block
depending on a run-time condition.
Sequence — A block of logic in which the instructions are executed in simple sequence, one
after another.
Sequential cohesion — A form of cohesion in which the modules form a chain of
transformations, with the output from one module serving as input to the next.
Span-of-control (breadth) — A measure of the number of modules directly controlled by a
higher-level routine.
Temporal cohesion — A type of cohesion in which the elements are related by time.
Transform process — A process that converts the input data to output form.
Trigger event — the event that activates a program or causes it to change from a wait state
to a run state.
27 Structured Programming

8.1 PROGRAMMING BASICS

8.1.1 Variables and data types

Variables and Data Types


Variables are the nouns of a programming language: they are the entities (values, data) that
act or are acted upon. The character-counting program uses two variables--count and args.
The program increments count each time it reads a character from the input source and
ignores args.
A variable declaration always contains two components: the type of the variable and its
name. Also, the location of the variable declaration, that is, where the declaration appears
in relation to other code elements, determines the scope of the variable.
Variable Types
All variables in the Java language must have a data type. A variable's type determines the
values that the variable can have and the operations that can be performed on it. For
example, the declaration int count declares that count is an integer (int). Integers can have
only whole number values (both positive and negative) and you can use the standard
arithmetic operators (+, -, and so on) on integers to perform the standard arithmetic
operations (addition, subtraction, and so on). There are two major categories of data types
in the Java language: primitive types and reference types.
Primitive types contain a single value and include types such as integer, floating point,
character, and Boolean. The following table lists, by keyword, all of the primitive data types
supported by Java, their size and format, and a brief description of each.
Type Size/Format Description
(whole numbers)
byte 8-bit two's complement Byte-length integer
short 16-bit two's complement Short integer
int 32-bit two's complement Integer
long 64-bit two's complement Long integer

(real numbers)
float 32-bit IEEE 754 Single-precision floating point
double 64-bit IEEE 754 Double-precision floating point

(other types)
Char 16-bit Unicode character A single character
Boolean true or false a Boolean value (true or false)
Reference types are called such because the value of a reference variable is a reference (a
pointer in other terminology) to the actual value or set of values represented by the
variable. For example, the character-counting program declares (but never uses) one
variable of reference type, args, which is declared to be an array of String objects. When
used in a statement or expression, the name args evaluates to the address of the memory
location where the array lives. This is in contrast to the name of a primitive variable, the
count variable, which evaluates to the variable's actual value.

FOR FULLNOTES
Besides arrays, classes and interfaces are also reference types. Thus when you create a class
or interface you are in essence defining a new data type. See Objects, Classes, and

CALL/TEXT:0724 962 477


Interfaces for information about defining your own classes and interfaces.
28 Structured Programming

Note to C and C++ Programmers: There are three C Data Types Not Supported By the Java
Language. They are pointer, struct, and union. These data types are not necessary in Java;
you use classes and objects instead.

Variable Names
A program refers to a variable's value by its name. For example, when the character-
counting program wants to refer to the value of the count variable, it simply uses the name
count. By convention, variable names begin with a lower case letter (class names begin with
a capital letter).

In Java, a variable name:

• Must be a legal Java identifier comprised of a series of Unicode characters. Unicode


is a character coding system designed to support text written in diverse human
languages. Unicode allows for the codification of up to 65,536 characters (currently
34,168 have been assigned). This allows you to use characters in your Java programs
from various alphabets such as Japanese, Greek, Russian, Hebrew, and so on. This is
important so that programmers can write code that is meaningful in their native
languages.
• must not be the same as a keyword or a Boolean literal (true or false)
• must not have the same name as another variable whose declaration appears in the
same scope
Rule #3 implies that variables may have the same name as another variable whose
declaration appears in a different scope. This is true. In addition, in some situations, a
variable may share names with another variable which is declared in a nested scope.
By convention, variables names begin with a lower case letter. If a variable name is
comprised of more than one word, such as is Visible, the words are joined together and
each word after the first begins with an upper case letter.

Scope
A variable's scope is the block of code within which the variable is accessible. Also, a
variable's scope determines when the variable is created and destroyed. You establish the
scope of a variable when you declare it. Scope places a variable into one of these four
categories:

• member variable
• local variable
• method parameter
• exception handler parameter
A member variable is a member of a class or an object and is declared within a class (but not
within any of the class's methods). The character-counting program declares no member
variables.
Local variables are declared within a method or within a block of code in a method. In the
character-counting example, count is a local variable. The scope of count, that is, the code
that can access count, extends from the declaration of count to the end of the main()
method (indicated by the first right curly bracket ('}') that appears in the sample code). In
29 Structured Programming

general, a local variable is accessible from its declaration to the end of the code block in
which it was declared.

Method parameters are formal arguments to methods and constructors and are used to
pass values into methods and constructors. The discussion about writing methods on
the Implementing Methods page in the next lesson talks about passing values into methods
and constructors through method parameters. In the character-counting example, args is a
method parameter to the main() method. The scope of a method parameter is the entire
method or constructor for which it is a parameter. So, in the example, the scope of args is
the entire main method.

Exception handler parameters are similar to method parameters but are arguments to an
exception handler rather than to a method or a constructor. The character-counting
example does not have any exception handlers, so it doesn't have any exception handler
parameters. Handling Errors using Exceptions talks about using Java exceptions to handle
errors and shows you how to write an exception handler with its parameter.

Variable Initialization
Local variables and member variables can be initialized when they are declared. The
character-counting program provides an initial value for count when declaring it: int count =
0;
The value assigned to the variable must match the variable's type.
Method parameters and exception handler parameters cannot be initialized in this way. The
value for a parameter is set by the caller
Additional notes

At the core of any program are variables. Variables are where the dynamic information is
stored. When you type your name into a web form and send it, your name is a variable.

Not all variables are the same though. In fact, there are many different types of variables
that nearly every programming language has. Let’s look at a small selection of them, as well
as their short names if they have one:

Character (char): This is a single character, like X, £, 4, or *. You don’t often create single
character variables, but they are at the core of the language so you need to know what they
are String: This is a “string” of characters (see how they’re at the core?) of any length. In my
previous example – your name on web form – your name would be stored as a String
variable.

Integer (int): A whole number – whole meaning there are no digits after a decimal point. So
65 would be a valid integer; 65.78 would not.

Floating-point number (float): A number that may have digits after the decimal place. 65.00
is technically a floating point number, even though it could be represented just as easily as
an integer as 65. It takes more memory to store a float, which is why there is a distinction
FOR FULLNOTES
instead of just creating a “number” data type.

CALL/TEXT:0724 962 477


30 Structured Programming

Boolean (bool): A variable to represent true or false (or it could also mean 0 or 1, on or off).
The simplest data type and commonly used – get used to this one!

Array: These are essentially lists of other variables. There are a variety of array types
depending on the language, but basically they’re just a collection of variables in a sequential
list. For example: 1, 2,3,4,5 might be stored as an array (of length 5) containing integer
variables. Each variable in the array can then be accessed using an index – but you should
know the first item in the list has an index of 0 (yes, that can be confusing sometimes). By
storing them as an array, we make it easy to send a collection of variables around the
program and do things with them as a whole – such as counting how many things are in the
array or doing the same thing to each item (which is called an iteration, and we’ll get to that
another time). You should also know that a string is actually just an array of characters.

Strong and Weak Typed:

Moving on, programming languages can be divided into those that are strongly-typed, and
those that are weakly-typed. A strongly typed language (such as Java) requires that you
explicitly declare what type of variable you are creating, and they get very upset if you start
trying to do things with them that you shouldn’t. For example, a strongly typed language
would give you errors if you tried to add an integer and a string together. “How on earth am
I supposed to mathematically add together a word and a number?” it would cry – even
though you as a human clearly understand a string “5” is semantically the same as an
integer with the value of 5.

A weakly typed language on the other hand would just say “whatever”, and give it a shot
without complaint – but the answer could go either way. Perhaps “5+5” = 10, perhaps it’s
“55” – who knows! It might seem at first like weakly-typed languages are easier to write, but
they can often result in curious errors and unexpected behavior that take you a while to
figure out.

Assignment and Equality:

Nothing to do with socialism…Instead, it’s a concept that catches out many programming
newbies so I wanted to address it now. There is a difference between assigning and testing
for equality. Consider the following, both of which you would probably read as “A is equal
to 5”:

A = 5;
A == 5;

Can you tell the difference? The first is known as assignment. It means assign the value of 5
to variable A. You are “setting” the variable value. The second statement is one of equality.
It’s a test – so it actually means “is A equal to 5?” – The answer given back to you would be a
Boolean value, true or false. You’ll see how this can mess up your programs in later lessons.
THIS IS A FREE SAMPLE OF THE ACTUAL NOTES

TO GET FULL/COMPLETE NOTES:

Call/Text/Whatsapp:0724 962 477

You can also write to us at: 24sevenstudy@gmail.com

To download more resources visit: http://www.mykasnebnotes.com

For updates and insights like us on Facebook

FOR FULLNOTES
CALL/TEXT:0724 962 477

Das könnte Ihnen auch gefallen