FLOWCHART Lecture
FLOWCHART Lecture
ENGG 0304
WHAT IS A FLOWCHART?
START
Display message
“How many
hours did you
work?”
Read PayRate
in Program 1-1.
GrossPay
END
BASIC SYMBOLS START
Display message
Terminal
“How many
hours did you
work?”
• indicate a starting or
ending point
Read PayRate
Multiply Hours
by PayRate.
Display
GrossPay
END Terminal
END
BASIC SYMBOLS START
Display message
“How many
hours did you
work?”
Multiply Hours
by PayRate.
Display message Store result in
GrossPay.
“How many
Read Hours
hours did you Display
GrossPay
work?”
END
BASIC SYMBOLS START
Display message
“How many
hours did you
work?”
as a mathematical
computation or variable
Read PayRate
GrossPay.
END
FOUR FLOWCHART
STRUCTURES
• Sequence
• Decision
• Repetition
• Case
SEQUENCE STRUCTURE
• A series of actions are performed in
sequence
• The pay-calculating example was a
sequence flowchart.
DECISION STRUCTURE
• The flowchart segment below shows how
a decision structure is expressed in C++
as an if/else statement.
Flowchart C++ Code
NO YES if (x < y)
x < y? a = x * 2;
else
Calculate a Calculate a a = x + y;
as x plus y. as x times 2.
DECISION STRUCTURE
• The flowchart segment below shows a
decision structure with only one action to
perform. It is expressed as an if
statement Flowchart
in C++ code.
C++ Code
NO YES
x < y? if (x < y)
a = x * 2;
Calculate a
as x times 2.
REPETITION STRUCTURE
• The flowchart segment below shows a
repetition structure expressed in C++ as
a while loop.
Flowchart C++ Code
while (x < y)
YES x++;
x < y? Add 1 to x
CONTROLLING A REPETITION
STRUCTURE
• The action performed by a repetition structure
must eventually cause the loop to terminate.
Otherwise, an infinite loop is created.
• In this flowchart segment, x is never changed.
Once the loop starts, it will never end.
• QUESTION: How can this
flowchart be modified so YES
x < y?
it is no longer an infinite Display x
loop?
CONTROLLING A REPETITION
STRUCTURE
• ANSWER: By adding an action within the
repetition that changes the value of x.
YES
x < y? Display x Add 1 to x
CASE STRUCTURE
If years_employed = 2, If years_employed = 3,
bonus is set to 200 bonus is set to 400
1 2 3 Other
END
A
MODULES
START
structures combined.
Display “ x is
outside the limits”
NO YES
x < max?
Display “ x is Display “ x is
outside the limits” within limits”
ANSWER
• What do each of the following symbols
represent?
Decision
Terminal
Input/Output
Operation Connector
Module
Process
REVIEW
• Name the four flowchart structures.
ANSWER
• Sequence
• Decision
• Repetition
• Case
REVIEW
• What type of structure is this?
ANSWER
• Repetition
REVIEW
• What type of structure is this?
ANSWER
• Sequence
REVIEW
• What type of structure is this?
ANSWER
• Case
REVIEW
• What type of structure is this?
ANSWER
• Decision
TRY: CLASS AVERAGE
ALGORITHM
• Problem: Calculate and report the grade-point
average for a class
• Discussion: The average grade equals the sum
of all grades divided by the number of students
• Output: Average grade
• Input: Student grades
• Processing: Find the sum of the grades; count
the number of students; calculate average
START
START
Initialize
counter and
sum to 0
START
Initialize
counter and
sum to 0
Are
no there yes
more
data?
Average = Get next
sum/counter grade
START
Initialize
counter and
sum to 0
Are
no there yes
more
data?
Average = Get next
sum/counter grade
Increment
counter
START
Initialize
counter and
sum to 0
Are
no there yes
more
data?
Average = Get next
sum/counter grade
Increment
counter
Add grade to
sum
START
Initialize
counter and
sum to 0
Are
no there yes
more
data?
Average = Get next
sum/counter grade
Increment
counter
Add grade to
sum
START
Initialize
counter and
sum to 0
Are
no there yes
more
data?
Average = Get next
sum/counter grade
Display Increment
average counter
Add grade to
sum
START
Initialize
counter and
sum to 0
Are
no there yes
more
data?
Average = Get next
sum/counter grade
Display Increment
average counter