Computer Programming Slide 3
Computer Programming Slide 3
Introduction to Flowcharting
Anup Majumder
Assistant Professor
Department Of Computer Science and Engineering
Jahangirnagar University
2
START
of a program.
Read PayRate
Multiply Hours
by PayRate.
Store result in
GrossPay.
Display
GrossPay
END
3
Basic Flowchart START
Rounded
Rectangle
– rounded rectangles
Read PayRate
– parallelograms
– a rectangle Multiply Hours
by PayRate.
Rectangle Store result in
• Each symbol represents GrossPay.
operation. Rounded
Rectangle
GrossPay
END
4
Basic Flowchart START Terminal
Multiply Hours
by PayRate.
START Store result in
GrossPay.
Display
GrossPay
END Terminal
END
5
Basic Flowchart START
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
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
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
Read PayRate
Multiply Hours
by PayRate.
Store result in
Variable Contents: GrossPay.
Hours: ? Display
PayRate: ? GrossPay
GrossPay: ? END
9
Stepping Through START
Display message
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
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
Read Hours
How much
do you get
paid per
Display message
hour? 20
“How much do
you get paid per
hour?”
Hours: 40
The value 20 is stored in PayRate. Display
PayRate: 20 GrossPay
GrossPay: ? END
12
Stepping Through START
Display message
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
Display message
Read PayRate
Multiply Hours
by PayRate.
Store result in
Variable Contents: GrossPay.
Hours: 40
Step 6: An Output Display
PayRate: 20 Operation GrossPay
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
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
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