Flowchart and Pseudocode
Instructor: Khushnur Binte Jahangir
Programming
• A typical programming task can be divided into
two phases:
Problem solving phase
produce an ordered sequence of steps that
describe solution of problem
this sequence of steps is called an algorithm
Implementation phase
implement the program in some programming
language
Understanding the Program
Development Cycle
• Program development cycle
– Understand the problem
– Plan the logic
– Code the program
– Use software (a compiler) to translate the program into machine
language
– Execute/run the program
3
Using Pseudocode Statements
and Flowchart Symbols
• When we solve a problem there are some logical steps that
we follow
• Pseudocode
– English-like representation of those logical steps
• Flowchart
– Pictorial representation of those logical steps
– emphasizes individual steps and their interconnections
4
Writing Pseudocode
• Pseudocode representation of a number-doubling problem
start
input myNumber
set myAnswer = myNumber * 2
output myAnswer
stop
5
Example
• Example : Write a pseudocode to determine a
student’s final grade and indicate whether it is
passing or failing. The final grade is calculated as
the average of four marks.
Pseudocode
Pseudocode:
start
Input a set of 4 marks
Calculate their average by summing and dividing by 4
if average is below 50
Print “FAIL”
else
Print “PASS”
stop
Flowchart
Using Flowchart Symbols
• Input symbol
o Indicates input operation
o Parallelogram
9
Using Flowchart Symbols
• Processing symbol
o Processing statements such as
arithmetic
o Rectangle
10
Using Flowchart Symbols
• Output symbol
o Represents output statements
o Parallelogram
11
Drawing Flowcharts (continued)
• Flowlines
– Arrows that connect steps
• Terminal symbols
– Start/stop symbols
– Shaped like a racetrack
12
Flowchart Symbols and Pseudocode Statements
Flowchart and pseudocode of program that doubles a number
13
Flowchart
Problem
• Write a pseudocode and flowchart to find the
summation of two numbers.
Flowchart: Adding Two Numbers
Pseudocode
Step 1 : Start
Step 2 : Input first number A
Step 3 : Input second number B
Step 4 : Add the two numbers and
store it in total
Step 5 : Print Total
Step 6 : Stop
12-Feb-15 16
Problem
Write a pseudocode and draw a flowchart that
will read the two sides of a rectangle and
calculate its area.
Solution
• . Start
Pseudocode
Input W,L Step 1 : Start
Step 2 : Input W,L
Step 3 : [Compute] A= W * L
Step 4 : Print A
A = W*L Step 5 : Stop
Output A
End
Problem
• Write a pseudocode and flowchart to find the
area of a square.
Flowchart: Calculating area of a square
Start Pseudocode
Step 1 : Start
Input x Step 2 : Read value for x (side)
Step 3 : [Compute] Area = x * x
Step 4 : Output Area
Step 5 : Stop
Area = x*x
Output Area
End
20
Problem
Write a flowchart and pseudocode to find the
average of three numbers.
Flowchart: Find the average of three numbers
Flowchart Pseudocode
12-Feb-15 22
Understanding the Three Basic Structures
• Structure
– Basic unit of programming logic
– Sequence
• Perform actions in order
• No branching or skipping any task
– Selection (decision)
• Ask a question, take one of two actions
• Dual-alternative or single-alternative ifs
– Loop
• Repeat actions based on answer to a question
23
Understanding the Three Basic Structures
Sequence structure
24
Understanding the Three Basic Structures
(continued)
Selection structure
25
Understanding the Three Basic Structures
(continued)
• Dual-alternative if
– Contains two alternatives
– If-then-else structure
if someCondition is true then
do oneProcess
else
do theOtherProcess
26
Understanding the Three Basic Structures
(continued)
• Single-alternative if
if student answers the bonus questions correctly
then add extra 5 marks
– Else clause is not required
• null case
– Situation where nothing is done
27
Understanding the Three Basic Structures
(continued)
Single-alternative selection structure
28
Flow chart to find the larger of two numbers.
Pseudocode
Step1: Start
Step 2: Enter two numbers A and B
Step 3: Check if A is greater than B if yes go to
Step 4 else go to Step 5
Step 4: Print A is greater than B
Step 5: Check if B is greater than A if yes go to
Step 6 else go to Step 7
Step 6: Print B is greater than A
Step 7: Print A is equal to B
Step 8: Stop
29
Problem
• Draw a flowchart to find out if a number is
even or odd.
Solution
yes no
Problem
Draw a flow chart to find out if a number is
divisible by 5 or not?
.
Start
Input x
no x MOD 5 == yes
0?
Output “Not Output
Divisible by “Divisible by
5” 5”
End
Start
Decision Problem# 3
Input
x,y,z
no yes
Is y > z Is x > y Is x > z
yes no no yes
Output z
Output y Output x
End
Repetition in programming
• Sometimes we have to repeat some tasks in a
program
• Example: adding the same number multiple
times
• This is called a repetition
Loop Structure
• Loop structure
– Repeats a set of actions based on the answer to a
question
• Loop body
– Also called repetition or iteration
– Question is asked first in the most common form
of loop
– while … for
Loop Structure
Example
sum = 0
for i = 1 to 100
sum = sum + I
What are we trying to do?
Example
• while testCondition continues
to be true
do someProcess
• while temperature is high
keep the cooler running
Example
What is the output if n = 3?
The trick is to keep track
Continue..
What is the output if n = 3?
i sum
- 0
1 1
2 3
3 6
4 6
Example
Example
Find the output for n=4
Practice problem
Find the output sum when
N=128
Practice Problem
Draw a flow chart to print the number from 1 to
N.
Practice Problem
• Draw a flow chart to print the multiplication
table of 5.
• For example: 5 * 1=5
• 5* 2=10
• 5*3=15
• .
• .