Structured Programming
Objectives
1.To describe flowcharts
2.To show some standard symbols for flowcharts
3.To show examples of flowcharts 4.To practice using flowcharts
5.To describe pseudocode
6.To show examples of pseudocode 7.To practice using pseudocode
Introduction
• Before writing a program:
• Have a thorough understanding of the problem
• Carefully plan an approach for solving it
• While writing a program:
• Know what “building blocks” are available
Flowcharting
• Flowcharting is a method of designing a
solution to a problem by using symbols which indicate actions to be performed.
• It is customary to use geometric figures of
certain shapes to indicate the various kinds of acts.
Algorithm
• An algorithm is a well-defined sequence of
acts or procedures to perform for the solution of a problem in a finite number of steps.
Flowcharting Symbols
• START or STOP symbol
• Input/Output symbol representing one or more input/output operations • Processing symbol representing
• Decision symbol representing a branching point in the program
• Flowlines or arrows that indicate the next step of the flowchart
• In-Connector and
• Out-Connector used to show the flow from one part to another part of the program
1
Control Structures
• Bohm and Jacopini
• All programs written in terms of 3 control structures
• Sequence structures. Programs executed sequentially by default
• Selection structures: Java has three types: if, if/else, and case
• Repetition structures: Java has four types:
9
Control Structures
• Single-entry/single-exit control
structures
• Connect exit point of one control
structure to entry point of the next (control-structure stacking)
Control Structures - Sequence
• Each action is in a processing symbol by itself.
• The actions are performed in the sequence (following the arrows)
• Example : What do you do in the
morning?
• Brush teeth • Wash face • Comb hair
11
Control Structures-Transfer of
Control
• When the next statement executed is not the next one in sequence
The
if
Selection Structure
• Ex. If grade >= 60, print
something
• Diamond symbol (decision
symbol)
• Indicates decision is to be made
• Contains an expression that can
be true or false
• Test the condition, follow
13
The
if
Selection Structure
• if structure is a
single-entry/single-exit structure
true
false
grade >= 60 print “Passed”
A decision can be made on any expression.
zero - false nonzero - true Example:
The
if
/
else
Selection
Structure
• if
• Only performs an action if the
condition is true
• if/else
• Specifies an action to be
performed both when the
condition is true and when it is
false
15
The
if
/
else
Selection Structure
• Flow chart of the if/else selection structure
• Nested if/else structures
• Test for multiple cases by placing if/else selection structures inside if/else selection structures
• Once condition is met, rest of statements skipped
• Deep indentation usually not used in practice
true false
print “Failed” print “Passed”
The
while
Repetition Structure
• Repetition structure
• Programmer specifies an action to be repeated while some condition remains
true
• Psuedocode:
While there are more items on my shopping list Purchase next item and cross it off my list
• while loop repeated until condition becomes false
17
The
while
Repetition Structure
• Example:
int product = 2;
while ( product <= 1000 ) product = 2 * product;
product <= 1000 true product = 2 * product
Example 1:Calculating
Revenue
Suppose that we have the problem of calculating revenue which has the following specification
• Basic Service Charge: $25.00 • Unit Cable Cost: $2.00
The Beginning of the Flowchart
START
?
First Step-Input
START
Input
1.Basic Service Charge 2.Unit Cable Cost 3.Number of Installations
Processing
START
Input
1.Basic Service Charge 2.Unit Cable Cost 3.Number of Installations
4.Yards Of Cable
Process
Output
START
Input
1.Basic Service Charge 2.Unit Cable Cost 3.Number of Installations
4.Yards Of Cable
Process
Example 2:Averaging
Grades for 5 students
• Suppose that we have the problem of calculating the average grade for
each student in a class where five examinations have been given.
The Flowchart
Start
INPUT Five grades
Calculate the Average
Print the Result
Example 3:Averaging Final
Grades for all Students
• Suppose that the students in a
class took their final examination yesterday and the instructor has just finished grading the exam
and is studying the grades. • We want to start planning a
The Steps
• We begin to think about the
problem as we look at the grades in the grade book .
Start With the Input
Start
Next Step is Processing
Start
INPUT Five grades for the first student
Controlling the Flow
• Then some statement will be
needed to allow control to return to the top of the program to see if
there is another set of student records to process.
The Flowchart For
n
Students
Start
INPUT Five grades for the first student
Calculate the Average
More students ?
Yes
End
•When there are no more
Complete Flowchart For
n
Students
Start
INPUT Five grades for the first student
Calculate the Average
End
More students ?
Yes
Pseudocode
• Flowcharts were the first design tool to be widely used, but unfortunately they do not reflect some of the
concepts of structured programming very well.
• Pseudocode, on the other hand, is a newer tool and has features that make it more reflective of the structured concepts.
What is it?
• Pseudo code is a kind of structured english for describing algorithms.
• It allows the designer to focus on the logic of the algorithm without being distracted by details of language syntax.
• At the same time, the pseudo code needs to be complete.
It describes:
• the entire logic of the algorithm so that implementation becomes a rote
• mechanical task of translating line by line into source code.
Pseudocode
• Artificial, informal language that
helps us develop algorithms
• Similar to everyday English
• Not actually executed on
computers
• Helps us “think out” a program
before writing it
Rules for Pseudocode
• Write only one statement per line • Capitalize initial keyword
• Indent to show hierarchy • End multiline structures
One Statement Per Line
Each statement in pseudocode should express just one action for the computer. If the task list is properly drawn, then in most cases each task will correspond to one line of pseudocode.
Task List
Read name, hours worked, rate of pay
Perform calculations
Pseudocode
READ name, hoursWorked, payRate
Capitalize Initial Keyword
In the example below note the words: READ and WRITE. These are just a few of the
keywords to use, others include:
READ, WRITE, IF, ELSE, ENDIF, WHILE, ENDWHILE
Pseudocode
READ name, hoursWorked, payRate
gross = hoursWorked * payRate
Indent to Show Hierarchy
• Sequence:
Keep statements in sequence all starting in the same column
• Selection:
Indent statements that fall inside selection structure, but not the keywords that form the selection
• Loop:
Indent statements that fall inside the loop but not keywords that form the loop
Each design structure uses a particular indentation pattern
End Multiline Structures
In the IF/ELSE/ENDIF as constructed above, the ENDIF is in line with the IF.
The same applies for WHILE/ENDWHILE etc… READ name, grossPay, taxes
IF taxes > 0
net = grossPay – taxes ELSE
net = grossPay ENDIF
Language Independence
• Resist the urge to write in whatever language you are most comfortable with, in the long run you will save time.
The Selection Structure
amount < 100
interestRate = .06 interestRate = .10
yes no
IF amount < 100
interestRate = .06 ELSE
Interest Rate = .10 ENDIF
The Looping Structure
In flowcharting one of the more
confusing things is to separate selection from looping. This is because each
structure uses the diamond as their control symbol.
In pseudocode we avoid this by using specific keywords to designate looping
WHILE / ENDWHILE
WHILE count < 10 ADD 1 to count
REPEAT / UNTIL
UNTIL count >= 10 WRITE “The End”Mainline count = 0 REPEAT
DO Process UNTIL count >= 10
Advantages & Disadvantages
Easily modified
Implements structured
concepts
Done easily on Word
Processor
Flowchart
Disadvantages:
Hard to modify Structured design
elements not implemented
Special software required
Pseudocode
Disadvantages:
Not visual
No accepted standard,