Structured Programming

Structured Programming
CECS274
Spring 2009
Mimi Opkins

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
8.To compare pseudocode with
flowcharts

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
• Use good programming principles

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.
• In a flowchart these figures are diagrammed
with connecting arrows, indicating a certain
sequence for performing the acts in order to
solve a problem.

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.
• Therefore, a flowchart is an excellent vehicle
to represent an algorithm.

Flowcharting Symbols
• START or STOP symbol
• Input/Output symbol representing
one or more input/output operations
• Processing symbol representing
one or more arithmetic operations

• Decision symbol representing a
branching point in the program
• Flowlines or arrows that indicate the
next step of the flowchart
• In-Connector and
1


• Out-Connector used to show the flow
from one part to another part of the
1
program

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:
while, do/while, do/until and for

8

Control Structures
• Single-entry/single-exit control

structures
• Connect exit point of one control
structure to entry point of the
next (control-structure stacking)
• Makes programs easy to build

9

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?







10

Brush teeth
Wash face
Comb hair
Smile in mirror
Having breakfast

Control Structures-Transfer of
Control
• When the next statement executed is
not the next one in sequence
• Overuse of goto statements led to
many problems

11

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
appropriate path

12

The if Selection Structure
• if structure is a singleentry/single-exit structure

grade >= 60

 
false

13


true

print “Passed”

A decision can be made 
on any expression. 
zero - false 
nonzero - true
Example:
3 - 4 is true

The if/else Selection
Structure

• if

• Only performs an action if the
condition is true


• if/else
 

14

• Specifies an action to be
performed both when the
condition is true and when it is
false

The if/else Selection Structure
• Flow chart of the if/else
selection structure
false
print “Failed”

grade >= 60

true
print “Passed”


• 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
15

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

16

The while Repetition Structure
• Example:
int product = 2;
while ( product 0
net = grossPay – taxes
ELSE
net = grossPay
ENDIF
WRITE name, net

In the IF/ELSE/ENDIF as constructed above,
the ENDIF is in line with the IF.
The same applies for WHILE/ENDWHILE etc…

Language Independence


• Resist the urge to write in whatever language you
are most comfortable with, in the long run you will
save time.
• Remember you are describing a logic plan to
develop a program, you are not programming!

The Selection Structure
yes

amount