CS1102
Fundamentals of Programming
Lecture 2 (Flowcharts and Pseudocode)
2023/11/20
Bachelor of Information Technology
Department of Computing
Faculty of Computing and Technology
Saegis Campus
Nugegoda
Udaya Perera(MSc in IT(Reading),B.Eng SE, SQA 9 SE)
1
Flowcharts
and Pseudocodes
An algorithm is a representation of a solution to a problem.
In the problem-solving phase of computer programming,
you will be designing algorithms. These algorithms can be
designed though the use of flowcharts or pseudocode.
2
Flowcharts
A flowchart is a pictorial
representation of the logic. A
flowchart is a diagram made up of
boxes, diamonds and other shapes,
connected by arrows - each shape
represents a step in the process, and
the arrows show the order in which 3
Basic Symbols of a Flow Chart
✔ Process ✔ input/output
Indicates any type of Used for any Input / Output (I/O)
internal operation inside operation. Indicates that the
the Processor or Memory computer is to obtain data or
output results
4
Basic Symbols of a Flow Chart
✔ Flow Lines ✔ Decision
Shows direction of flow. Used to ask a question that can
Connect the steps be answered in a binary format.
Has two flow lines: (Yes/No,
True/False).
5
Basic Symbols of a Flow Chart
✔ Terminal ✔ Connector
Indicates the starting or Allows the flowchart to be
ending of the program, drawn without intersecting lines
process, or interrupt or without a reverse flow.
program. Show the sequence of
statements
6
Find the error
Example: Flow chart to
get
the summation of two
numbers
7
Back-pointing
Arrows
Back-pointing arrows
show statements that will
be repeated.
8
Infinite loop
A sequence of statements
that repeats forever with
no escape.
We can test for a
predetermined value that
means “stop processing”.
9
Sentinel value (or
dummy value)
A preselected value that
stops the execution of a
program. We can use
these values with input
from files or users.
10
End-of-file (EOF)
marker
Use this instead of
sentinel value for file
input. This marks the end
of data in a file that the
program reads.
11
Flowchart connector
symbol
• Marks a logic transfer to another location
in the flowchart
• Transfer location can be on the same page
or on another page
On-page symbol: A circle with a number or
letter to identify the matching transfer
location
Off-page symbol: A square with a pointed
bottom, containing page number and a
number of letters to identify the matching
transfer location
12
Four Flowchart Structures
•Sequence
•Decision
•Repetition
•Case
13
Sequence Structure
•A series of actions are performed in
sequence
14
Start
End 15
Decision Structure
Flowchart
• The flowchart segment below
NO YES shows how a decision structure is
x < y?
expressed.
Calculate Calculate
a as x plus a as x
y. times 2.
16
Decision Structure
Flowchart
NO YES
• The flowchart segment below
x < y? shows a decision structure with
only one action to perform.
Calculate
a as x
times 2.
17
Repetition Structure
Flowchart
• The flowchart segment below
YES shows a repetition structure
x < y? Add 1 to x
18
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
YES
flowchart be modified so x < y? Display x
it is no longer an infinite
loop?
19
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
20
Write a flow chart to make a cup of tea
21
Exercise
What is the output of
the given flowchart?
22
Iteration 1: Iteration 2:
i = 2 i = 4
print = 2+1 = 3 print = 4+1 = 5
print 3 print 5
i = 2+2 = 4 i = 4+2 = 6
Iteration 3:
i = 6
print = 6+1 = 7
print 7
Output: 3 5 7 i = 6+2 = 8
23
Exercise
Draw a flowchart to get the sum of numbers user enter. When the user enter -1
as the input the program should stop taking input and calculating the sum and
the program should display the sum
24
25
Flowchart Example
Example: N=3
26
Flowchart Example
Example: N=3
Step 1: F=1*1=1
Step 2: F=1*2=2
Step 3: F=2*3
F = 6
F = 1*2*3
From wikipedia.org 27
START
Input
Flowchart Example N
Sum =0
C=0
Input
number
Sum = Sum + number
Give the output when N = 3 C=C+1
C
No
=
N? Yes
A = Sum / N
Print
A
END 28
Example: N=3
Number = 2
Step 1: Sum = 0+2=2
C=0+1=1
Number = 6
Step 2: Sum = 2+6=8
C=1+1=2
Number = 4
Step 3: Sum = 8+4=12
C=2+1=3
Sum=2+6+4=12
A=12/3=4
Output: 4
29
Lab work 1
• Draw a flowchart to find the
product of two numbers using
only addition operation
30
31
Lab work 2
• Draw a flowchart to find the
division of two integer numbers
using only subtraction operation.
Find the division and remainder.
32
Example: a=4
b=2
Step 1: a = 4-2=2
m=0+1=1
Step 2: a = 2-2=0
m=1+1=2
Division = 2
Remainder = 0
33
Pseudocode
✔ Pseudocode is an English-like representation of the logical steps it takes
to solve a problem.
✔ Pseudo is a prefix that means false, and to code a program means to put
it in a programming language; therefore, pseudocode simply means false
code, or sentences that appear to have been written in a computer
programming language but do not necessarily follow all the syntax
rules of any specific language.
34
35
Some useful keywords that
you can use on
Pseudocodes
START: This is the start of your pseudocode.
INPUT: This is data retrieved from the user through typing or
through an input device.
READ / GET: This is input used when reading data from a data
file.
PRINT, DISPLAY, SHOW: This will show your output to a
screen or the relevant output device.
COMPUTE, CALCULATE, DETERMINE: This is used to
calculate the result of an expression.
SET, INIT: To initialize values
INCREMENT: To increase the value of a variable
DECREMENT: To reduce the value of a variable 36
Exercise
Write a pseudocode for multiplying a number by 2
37
38
Activity 1
Write pseudocode to find the sum of two numbers.
Start
Prompt user to enter two numbers
Store the first input in a variable
Store the second input in b variable
Calculate the sum of the two numbers
Display the result to the user
End
39
Activity 2
Write pseudocode to find the average of three numbers.
Start
Prompt user to enter three numbers
Store the first input in a variable
Store the second input in a variable
Store the third input in a variable
Calculate the sum of the three numbers
Divide the sum by 3 to find the average
Display the average to the user
End
40
Activity 3
Write pseudocode to check if a number is even or odd.
Start
Prompt user to enter a number
Store the input in a variable
Divide the number by 2 and check for the
remainder
If the remainder is 0, display that the
number is even
If the remainder is not 0, display that the
number is odd
End
41