You are on page 1of 43

Lecture 3

Introduction to Flowcharting

Anup Majumder
Assistant Professor
Department Of Computer Science and Engineering
Jahangirnagar University
2
START

What is a Flowchart? Display message


“How many
hours did you
work?”

• A flowchart is a Read Hours

diagram that depicts Display message


“How much do
the “flow of control” you get paid per
hour?”

of a program.
Read PayRate

Multiply Hours
by PayRate.
Store result in
GrossPay.

Display
GrossPay

END
3
Basic Flowchart START
Rounded
Rectangle

Symbols Display message


“How many
hours did you
work?”

• Notice there are three Read Hours

types of symbols in this Display message


“How much do
Parallelogram
flowchart: you get paid per
hour?”

– rounded rectangles
Read PayRate
– parallelograms
– a rectangle Multiply Hours
by PayRate.
Rectangle Store result in
• Each symbol represents GrossPay.

a different type of Display

operation. Rounded
Rectangle
GrossPay

END
4
Basic Flowchart START Terminal

Symbols Display message


“How many
hours did you
work?”

• Terminals Read Hours

– represented by rounded Display message


“How much do
rectangles you get paid per
hour?”
– indicate a starting or
ending point Read PayRate

Multiply Hours
by PayRate.
START Store result in
GrossPay.

Display
GrossPay

END Terminal
END
5
Basic Flowchart START

Symbols Display message


“How many
hours did you
work?”

• Input/Output Operations Read Hours

– represented by Display message


“How much do Input/Output
parallelograms you get paid per
Operation
hour?”
– indicate an input or output
operation Read PayRate

Multiply Hours
by PayRate.
Display message Store result in
GrossPay.
“How many
Read Hours
hours did you Display
GrossPay
work?”
END
6
Basic Flowchart START

Symbols Display message


“How many
hours did you
work?”

• Processes Read Hours

– represented by rectangles Display message


“How much do
– indicates a process such as you get paid per
hour?”
a mathematical
computation or variable Read PayRate

assignment
Multiply Hours
by PayRate.
Process Store result in
Multiply Hours GrossPay.
by PayRate.
Store result in Display
GrossPay
GrossPay.
END
7
Stepping Through START

Display message

the Flowchart “How many


hours did you
work?”

In the next seven slides we Read Hours


will step through each
symbol in the flowchart. We Display message
“How much do
will show the program you get paid per
hour?”
output and the contents of
the variables. Read PayRate

Multiply Hours
by PayRate.
Store result in
Variable Contents: GrossPay.

Hours: ? Display
PayRate: ? GrossPay

GrossPay: ? END
8
Stepping Through START Step 1: An
Output
Display message Operation

the Flowchart “How many


hours did you
work?”

Screen Output Read Hours


How many
hours did
you work?
Display message
“How much do
you get paid per
hour?”

Read PayRate

Multiply Hours
by PayRate.
Store result in
Variable Contents: GrossPay.

Hours: ? Display
PayRate: ? GrossPay

GrossPay: ? END
9
Stepping Through START

Display message

the Flowchart “How many


hours did you
work?”

How many
Step 2: An Input Read Hours
hours did Operation
you work? (User types 40) Display message
40
“How much do
you get paid per
hour?”

Read PayRate

Multiply Hours
by PayRate.
Store result in
Variable Contents: GrossPay.
The value 40 is stored in Hours.
Hours: 40
Display
PayRate: ? GrossPay

GrossPay: ? END
10
Stepping Through START

Display message

the Flowchart “How many


hours did you
work?”

Screen Output Read Hours


How much
do you get
paid per
Display message
hour?
“How much do
Step 3: An you get paid per
Output hour?”
Operation

Read PayRate

Multiply Hours
by PayRate.
Store result in
Variable Contents: GrossPay.

Hours: 40
Display
PayRate: ? GrossPay

GrossPay: ? END
11
Stepping Through START

Display message

the Flowchart “How many


hours did you
work?”

Read Hours
How much
do you get
paid per
Display message
hour? 20
“How much do
you get paid per
hour?”

Step 4: Input Read PayRate


Operation
(User types 20) Multiply Hours
by PayRate.
Store result in
Variable Contents: GrossPay.

Hours: 40
The value 20 is stored in PayRate. Display
PayRate: 20 GrossPay

GrossPay: ? END
12
Stepping Through START

Display message

the Flowchart “How many


hours did you
work?”

Read Hours
How much
do you get
paid per
Display message
hour? 20
“How much do
Step 5: The product of you get paid per
Hours times PayRate is hour?”
stored in GrossPay
Read PayRate

Multiply Hours
by PayRate.
Store result in
Variable Contents: GrossPay.

Hours: 40
Display
PayRate: 20 The value 800 is stored
GrossPay

GrossPay: 800 in GrossPay.


END
13
Stepping Through START

Display message

the Flowchart “How many


hours did you
work?”

Screen Output Read Hours


Your gross
pay is 800
Display message
“How much do
you get paid per
hour?”

Read PayRate

Multiply Hours
by PayRate.
Store result in
Variable Contents: GrossPay.

Hours: 40
Step 6: An Output Display
PayRate: 20 Operation GrossPay

GrossPay: 800 END


14
Three Flowchart Structures
• Sequence
• Decision
• Iteration

15
Sequence Structure
• A series of actions are performed in sequence
• The pay-calculating example was a sequence
flowchart.

16
Decision Structure
• One of two possible actions is taken, depending on
a condition.

17
Decision Structure
• A new symbol, the diamond, indicates a yes/no question. If
the answer to the question is yes, the flow follows one
path. If the answer is no, the flow follows another path

NO YES

18
Decision Structure
• In the flowchart segment below, the question “is x < y?” is
asked. If the answer is no, then process A is performed. If
the answer is yes, then process B is performed.

NO YES
x < y?

Process A Process B

19
Decision Structure
• The flowchart segment below shows how a decision
structure is expressed in programs as an if/else statement.

Flowchart Program

NO YES if (x < y)
x < y? a = x * 2;
else

Calculate a as Calculate a as a = x + y;
x plus y. x times 2.

20
Decision Structure
• The flowchart segment below shows a decision structure
with only one action to perform. It is expressed as an if
statement in a program.
Flowchart Program
if (x < y)
NO YES a = x * 2;
x < y?

Calculate a as
x times 2.

21
Iteration Structure
• An iteration structure represents part of the program that
repeats. This type of structure is commonly known as a
loop.

22
Iteration Structure
• Notice the use of the diamond symbol. A loop tests a
condition, and if the condition exists, it performs an action.
Then it tests the condition again. If the condition still
exists, the action is repeated. This continues until the
condition no longer exists.

23
Iteration Structure
• In the flowchart segment, the question “is x < y?” is asked.
If the answer is yes, then Process A is performed. The
question “is x < y?” is asked again. Process A is repeated
as long as x is less than y. When x is no longer less than y,
the iteration stops and the structure is exited.

YES
x < y? Process A

24
Iteration Structure
• The flowchart segment below shows an iteration structure
expressed in a program as a while loop.

Flowchart Program

while (x < y)

YES x++;
x < y? Add 1 to x

25
Controlling an Iteration Structure
• The action performed by an iteration 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
YES
flowchart be modified so
x < y? Display x
it is no longer an infinite
loop?

26
Controlling an Iteration Structure
• ANSWER: By adding an action within the iteration that
changes the value of x.

YES
x < y? Display x Add 1 to x

27
A Pre-Test Iteration Structure
• This type of structure is known as a pre-test iteration
structure. The condition is tested BEFORE any actions are
performed.

YES
x < y? Display x Add 1 to x

28
A Pre-Test Iteration Structure
• In a pre-test iteration structure, if the condition does not
exist, the loop will never begin.

YES
x < y? Display x Add 1 to x

29
A Post-Test Iteration Structure
• This flowchart segment shows a post-test
iteration structure.
• The condition is tested AFTER the actions Display x
are performed.
• A post-test iteration structure always Add 1 to x
performs its actions at least once.

YES
x < y?

30
A Post-Test Iteration Structure
• The flowchart segment below shows a post-test iteration
structure expressed in a program as a do-while loop.

Program
Display x
do
{
Flowchart Add 1 to x
printf(“%d”,x);
x++;
} while (x < y);

YES
x < y?

31
Relationship Between Pre-Test
and Post-Test Iteration

32
Class Average Problem
• 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

33
Flowchart

34
Connectors
• Sometimes a flowchart will not fit on one
page.
• A connector (represented by a small circle)
allows you to connect two flowchart
segments.

35
Connectors

•The “A” connector


A
indicates that the second START

flowchart segment begins


where the first segment
ends.

END
A

36
Modules
• A program module, such as a subprogram
(or function in a program), is represented by
a special symbol.

37
Modules
START
•The position of the module
symbol indicates the point the Read Input.
module is executed.
•A separate flowchart can be Call calc_pay
constructed for the module. function.

Display results.

END

38
Combining Structures
• Structures are commonly combined to create more
complex algorithms.
• The flowchart segment below combines a selection
structure with a sequence structure.

YES
x < y? Display x Add 1 to x

39
Combining Structures
• This flowchart segment
shows two selection NO YES
structures combined. x > min?

Display “x is NO YES
outside the limits.”
x < max?

Display “x is Display “x is
outside the limits.” within limits.”

40
Exercise
• Draw a flowchart to input two numbers from user and
display the largest of two numbers

41
Exercise
• Find the sum of 5 numbers

42
Exercise
• Draw a flowchart to log in to facebook
account

43

You might also like