(Program Flow Chart) : 5.2.1 Sequence
(Program Flow Chart) : 5.2.1 Sequence
Introduction
Program Flow Chart is a graphical representation of the program logic in solving
a problem. It uses the standard symbols in designing input, process, output of a
program. The program flow should be read from top to bottom, left to right.
Processing (rectangle)
Decision (diamond)
Terminal (racetrack)
Connector (circle)
Program Flow
5.2.1 Sequence
E.g.
DO
S1: GET number1
1
S2: GET number2
S3: answer = number1 / number2
S4: DISPLAY answer
ENDDO
Start
Get
number
1
Get
number
2
answer =
number1 /
number2
Display
answer
Stop
The above sequence must be followed in order to print out the answer. You get
two numbers, compute the answer which is the division of number1 over
number2. Eventually, after the computation, the answer is printed out.
5.2.2 Selection
2
E.g. 1
DO
GET number1
GET number2
IF number2 = 0 THEN
DISPLAY "Division by zero error"
ELSE
DO
answer = number1/number2
DISPLAY answer
ENDDO
ENDDO
Start
Get
number
1
Get
number
2
yes
numbe Display
r2= 0? "Division
by zero
error"
no
answer =
number1 /
number2
Display
answer
Stop
3
The decision point or condition above is whether number2 is a zero. If condition
is true, i.e. number2 is a zero then the message of "Division by zero error" will
be displayed. If condition is false, i.e. number2 is not a zero, then computation
of number1 divide by number2 is carried out. The result of answer is then
displayed.
5.2.3 Iteration
Repeat a set of instructions a number of times based on condition stated. This loop
will end when condition has been fulfiled.
E.g. 1
DO
total = 0
GET number
WHILE number < 1000
DO
total = total + number
GET number
ENDDO
DISPLAY total
ENDDO
Start
total = 0
Get
number
no
numbe Display
total Stop
r<
1000?
yes
total = total +
number2
The condition stated above is whether number < 1000. If condition is true, i.e.
number is less than 1000, then number is accumulated as the total. After which,
4
you get another number. The next execution is going back to the while condition
to check if the condition is true, if yes, you accumulate the current number with
the previous numbers and get another number.
The loop or set of instructions will end when number is >= 1000, whereby the
accumulated total will be displayed.
E.g. 2
FOR i = 1 TO 12
DO
result = i * 9
DISPLAY (i, "* 9 = " , result)
ENDDO
Start
i=1
no
i <= Stop
12?
yes
result = i * 9
Display (i,
"*9=",result)
i=i+1
A variable i is initialised to 1 and eventually be incremented by 1. If i is in
between 1 to 12 inclusive, a processing of i will be performed and the result
displayed. The followings are the expected output :
1*9=9
5
2 * 9 = 18
3 * 9 = 27
4 * 9 = 36
5 * 9 = 45
6 * 9 = 54
7 * 9 = 63
8 * 9 = 72
9 * 9 = 81
10 * 9 = 90
11 * 9 = 99
1 2* 9 = 108
5.3 Connectors
When you have a program where the logic cannot be represented in a flowchart
than can fit on one page, you use connectors to connect the flowcharts on
different pages. However, the use of connectors should be avoided if possible as
the logic of the program is more difficult to follow when they do not fit on a
page.
For instance, if a flowchart has six processing steps and a page can fit only three
steps, the connector come into use.
Start 1
Step 1 Step 4
Step 2 Step 5
Step 3 Step 6
1 Stop
page 1 page 2
On page 1, the number assigned in the connector matches up the number assigned in the
connector on page 2 to determine the next step in the flowchart.
Exercises
6
page 1
1. You, along with your friends, are having your favourite dinner tonight. It's starting
with soup. The main course is meatloaf, mashed potatoes and peas. In addition, you can
have your choice of two deserts: apple pie or chocolate cake. Unfortunately, there's just
enough dessert for you to select a piece of one or the other, not both.
Draw a flowchart showing the order in which you will be served the food items at dinner.
Start your flowchart with sitting down to dinner and end it with clearing your plate off
the table when you're through. Assume there is only enough food for one serving of each
item.
2. Draw a flowchart to represent the logic of a program that accept the input of a
series of number. The program will display "Even number" if number entered is
even number. Otherwise the program will display "Odd number".
3. Add the following logic to the flowchart that you have created in question 1.
The program will add up all even number until 999 is entered. The number of
data entered, number of even numbers and the total of even numbers will be
displayed.
5. Assume you have bank account that compounds interest on a yearly basis. For
example, if you deposit $100 for two years at 4 percent interest, at the end of one
year you will have $104. At the end of two years, you will have the $104 plus 4
percent of that, or $108.16. Using program flowchart, draw the logic for a
program that would read in a deposit amount, a term, and an interest rate, and
print out your running total for each year of the term.
6. Draw a flowchart for a program that accepts an amount entered through the
keyboard. Then, determine the discount rate allowed; 20% would be given to
any purchase of more than $1000, 10% to purchase of more than $500
(inclusive). Display the discount given and also the amount after the deduction
of the discount.