FLOWCHARTS
Step-form (algorithm) and pseudo code program designs are both text-based, the statements
are written. Flow charts are a graphical method of designing programs and once the rules are
learned are very easy to draw.
The problem can either be solved by writing the steps or drawing to show the details that is
using pseudo code or flowchart.
To draw flowchart, we employ the symbols shown in the Figure 1. The meaning of each
flowchart symbol is given in Table 1.
Table 1: Meaning of flowchart symbols
Symbol name Description
Terminal(start, end) Indicates the beginning and end of an algorithm/program
Input/Output Indicates an input or output operation
Process Indicates calculation or assigning of a value to a variable
Flow lines Connects the flowchart symbols and indicates the logic flow
Decision Indicates a question that can be answered yes or no (true or false)
Types of flowcharts
-Programming flowcharts
-Systems flowcharts
-Data flow
1).Programming Flowcharts
Flow charts are a graphical method of designing programs.
There are three structures to be looked at when dealing with programming flow charts. These
are
Sequence structure: There are no decisions to make, no choices between “yes” or
“no”
Examples
Input variable: always on the right
Output variable: always on the left and it’s storage for input variables
NB: We only input variables not constants i.e. the ones that can change are the one we input
not numbers.
1). C = A+B 2) Gao = Obe +Boi – Two 3). I = Y +B
Z = A-I
Decision/Selection structure
There are two path to be followed either “yes” or “no”.
There are certain statements to be looked at here
-Simple IF
-Double IF (IF -Then-Else)
-Multiple IF (Else IF
Simple IF statement
IF statement answers the question, “Is this true or false?”, then proceeds on some action
based on this. Only one option/action is to be taken.
Example of the simple IF statement
1. C =a + b n>4
pseudo code
Start
Input n, a, b
If n>4 then
C=a+b
Endif
Print C
Stop
pseudo code structure for the simple IF statement
.
.
.
If condition then
Process
Endif
.
.
Flowchart structure for the simple IF statement
Three keywords to known when dealing with simple IF statement
-IF
-Then
-End if
Double if statement (Known as IF-THEN-ELSE.)
Two options are to be taken.
Example
Given that n>5 do the process C = a + b otherwise do the process C = a – b
Pseudo code
Start
Input n, a, b
If n>5 then
C=a+b
Else
C=a-b
End if
Print C
Stop
Pseudo code structure for the Double if statement
.
.
.
If condition then
Process1
Else
Process2
End if
.
.
.
Flowchart structure for the Double if statement
Multiple if statement (Else If)
More options to taken.
Example n = 8
n>8
n<8
pseudo code
Start
Input n, a, b
If n = 8 then
C=a+b
Else if n > 8 then
C=a–b
Else
C=a*b
Endif
Endif
Print C
Stop
Pseudo code structure for the Multiple if statement
.
If condition then
Process1
Else If condition then
Process2
Else
Process3
End if
End if
.
Flowchart structure for the Multiple if statement
Exercise
1. Y > 5 do the process C = a * b otherwise do the process C = a + b
2. Suppose you are given student marks and suppose to draw the multiple If flowchart.
Student mark>=60 “Good”
Student mark>=50 “Credit”
Student mark>=40 “Pass”
Student mark>=30 “Fail”
Repetition structures
The repetitive (looping) statements take care of frequency of execution of different segments of a
program.
Why is repetition needed?
Suppose you want to add five numbers to find their average. From what you have learned so far, you
know that you could proceed as follows;
Input num1 // get the first number
Input num2 // get the first number
Input num3 // get the first number
Input num4 // get the first number
Input num5 // get the first number
Sum = num1 +num2 +num3 +num4 +num5
Average = sum / 5
But suppose you want to add and find the average of 1000 or more numbers. You would list many
input statements, perhaps again in the output statements. This would take a lot of typing as well as
time. Also, if you wanted to run this program again with different values, or with a different number
of values, you would have to rewrite the program.
By using repetition you can add any set of numbers, without listing any additional input statements
and without changing the code.
There are certain statements used in repetition:
While
For next
While loop
A variable number is initialized and then the Do While Loop starts. First, the
condition is tested; if condition is true, then the statements are processed. When it gets
to the Loop it goes back to the Do and tests condition again. If condition is False on
the first pass, the statements are never processed.
Pseudo code structure for the while loop
While condition do
Process1
Process2
Process n
End while
Flowchart structure for the while loop
Process
Condition
True
False
Example
Start
X=0
While X< 4 DO
Input A
C= A + X
Print C
X=X+1
End While
Stop
For next
Pseudo code structure for for next
For counter variable = initial value to final value
Process/statements
Example
For c = 1 to 5 do
Print c
Next c
Update statement
Process
Condition
True
False
For i = -1 to 128
S=S+i
Next i
Example 2
For I = 1 to n step 2
Sum = sum + n
Next i
TRACE TABLE
The table we use to follow the flowchart e.g. representing variables with numbers.
EXAMPLES
1. Start
X=0
While X< 4 DO
Input A
C= A + X
Print C
X=X+1
End While
Stop
X A C
0 - -
1 3 4
2 4 6
3 5 8
4 2 8
NB: the bolded ones are to be found by students
2. Start
N=0
While N< 5 DO
Input Q, P
If Q >4 then
Dat = P + Q-N
Else
Dat = P + Q +N
End if
Print Q,Dat
N= N+1
End While
Stop
N Q P Dat
0 - - -
1 3 3 7
2 7 4 13
3 4 8 15
4 -2 5 7
5 8 1 7
Subprograms
A subroutine (also called procedure, function, routine, method, or subprogram) is a
portion of code within a larger program that performs a specific task and is relatively
independent of the remaining code.
As the name "subprogram" suggests, a subroutine behaves in much the same way as a
computer program that is used as one step in a larger program or another subprogram.
A subroutine is often coded so that it can be started ("called") several times and/or
from several places during a single execution of the program, including from other
subroutines, and then branch back (return) to the next instruction after the "call" once
the subroutine's task is done.
Importance of subprograms
Subroutines are a powerful programming tool and the syntax of many programming
languages includes support for writing and using them.
The use of subroutines (for example, through the structured programming approach);
Will often substantially reduce the cost of developing a program.
Easy to maintaining a large program.
Increasing program quality and reliability.
Annotations
An annotation is a note that is made while reading any form of text. This may be as simple
as underlining or highlighting passages, but in programming, comments are written
following syntax of a particular language. For example, in Basic programming keyword REM
(short form for remark) is used or an apostrophe, followed by a comment.
Annotated programs give descriptions about how each program instruction is useful to the
programmer in constructing a program. Creating these comments, usually a few sentences
long, establishes a summary for and expresses the relevance of each program line.