11-Programming Concepts-Flowcharts 2
11-Programming Concepts-Flowcharts 2
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.
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
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
Start
Input n, a, b
If n>5 then
C=a+b
Else
C=a-b
End if
Print C
Stop
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
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.
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;
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.
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.
While condition do
Process1
Process2
Process n
End while
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
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
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);
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.