Sunteți pe pagina 1din 31

Introduction To

Flowcharting
by
Jumail Bin Taliba
Faculty of Computer Science & Information System

Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 1
Today’s Topics
• Flowchart Symbols
• Control Structures
• Some examples

Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 2
Flowchart:

Represents an algorithm in graphical symbols

Example: Algorithm for multiplying two


numbers
Start

Get A
Get B

Calculate Resut
C=A*B

Display the
Result C

Stop

Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 3
Flowchart Symbols
Terminal: Used to indicates the start and end of a
flowchart. Single flow line. Only one “Start” and “Stop”
terminal for each program. The end terminal for
function/subroutine must use “Return” instead of “Stop”.
Process: Used whenever data is being manipulated. One
flow line enters and one flow line exits.
Input/Output: Used whenever data is entered (input) or
displayed (output). One flow line enters and one flow line
exits.
Decision: Used to represent operations in which there are
two possible selections. One flow line enters and two flow
lines (labeled as “Yes” and “No”) exit.
Function / Subroutine: Used to identify an operation in a
separate flowchart segment (module). One flow line enters
and one flow line exits.
On-page Connector: Used to connect remote flowchart
portion on the same page. One flow line enters and one
flow line exits.
Off-page Connector: Used to connect remote flowchart
portion on different pages. One flow line enters and one
flow line exits.
Comment: Used to add descriptions or clarification.

Flow line: Used to indicate the direction of flow of


control.

Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 4
Example:
Start Terminal.
Start Program starts
here

Read A Input.
Read B Enter values for
A and B

Calculate Resut
C=A*B Process

Display the
Result C Output

Stop Terminal
Stop Program ends
here

Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 5
Comments or description

Start

Read N, N = The number of students


M M = The number of subjects

Yes

No

Stop

Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 6
Connectors on the same page

Start

1 2

1- connection on the same flowchart portion

2- connection on the different flowchart portion

Stop

Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 7
Connectors on a different page

Page 1 Page 2

Start

2
1

Stop
Yes 1

No

Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 8
The detail of how the function works
is put in another flowchart.
Function
This is known as Function-Definition

Page 1 Start terminal for a Page 2


Function is different.
Start Do not use “Start”
AVRG ( result,n1, n2,n3)

Read
n1, n2 , n3

sum = n1+ n2+n3

Body of a function is
AVRG (result, n1, n2,n3) the same with
normal flowchart

At this point, result = sum/3


we only focus on what
to do. How to do it,
it comes later. Print
result
This part is known as
Function-Call Return

Stop
End terminal
must be a “Return”

This flowchart calculates the average of three numbers

Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 9
Parameters used in a function-definition
Related terms and concepts are called formal parameters

R, a, b, c are formal parameters


Page 1 Page 2

AVRG is the function name Start

Objects enclosed by ( ) – result, AVRG ( R, a, b,c)


n1, n2, n3 - are called parameters

Read
n1, n2 , n3

sum = a+ b+c

AVRG (result, n1, n2,n3)

Each formal parameter represents


an actual parameter according
to its order: R = sum/3
R represents result,
Print a represents n1,
Parameters used in a function-call
result b represents n2,
are called actual parameters c represents n3
Return
result, n1, n2, n3 are actual parameters The name of an actual parameter may be
different from its formal parameter
Stop

This flowchart calculates the average of three numbers

Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 10
Related terms and concepts (cont.) In a function-definition, you should only
use formal parameters – R, a, b, c

Page 1 You shouldn’t usePage


actual
2 parameters

Start

AVRG ( R, a, b,c)

Read
n1, n2 , n3

sum = n1 + n2 + n3

AVRG (result, n1, n2,n3)


This is wrong!
n1, n2, n3 are actual parameters.
Should use a, b, c instead.
R = sum/3

Print
R

Return
This is wrong!
R is an formal parameters.
Should use Stop
result instead.

Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 11
Related terms and concepts (cont.)

Page 1 Page 2

Start

AVRG ( R, a, b,c)
Read
n1, n2 , n3

A function may be Read


called more than once n4, n5 , n6 sum = a+ b+c

AVRG (average1, n1, n2,n3)


At this time:
R represents average1,
AVRG (average2, n4, n5, n6)
a represents n1,
b represents n2, R = sum/3
c represents n3
result = (average1 +average2) / 2
When comes to this:
R represents average2,
Return
Print
a represents n4,
result b represents n5,
c represents n6

Stop

This flowchart calculates the average of three numbers

Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 12
Related terms and concepts (cont.)

A function parameter may act as:

• Input
Data of the function

• Output
The result of the function

• Both

Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 13
Function definition:
Related terms and concepts (cont.)
R is the output parameter
a, b, c are input parameters
Page 1 Page 2

Start

AVRG ( R, a, b,c)

Read
n1, n2 , n3

sum = a+ b+c

AVRG (result, n1, n2,n3)

R = sum/3

Function call:
Print
result
result is the output
parameter.
n1, n2, n3 are the input Return
parameters.

Stop

This flowchart calculates the average of three numbers

Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 14
Function definition:
Related terms and concepts (cont.)
x and y act as both input and output
parameters
Page 1 Page 2

Start EXCHG( x, y)

Read hold = x
p, q

x=y

EXCHG (p, q)
y = hold

Print Return
p, q

Function call:

Stop p and q act as both input and output


parameters.

This flowchart exchanges or swaps the value


of x and y each other

Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 15
Related terms and concepts (cont.)

If there is only one output parameter, the flowchart may “RETURN” the result

Example: let take a look again at the function that calculates the average of three numbers.

Original function flowchart: Since it has only one output, the output is “RETURN”

Page 2 Page 2

AVRG ( R, a, b,c) AVRG ( a, b,c)

The output parameter (R)


is removed from the formal
parameter list
sum = a+ b+c sum = a+ b+c

R = sum/3 R = sum/3

and the result


is return
Return Return R

Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 16
Related terms and concepts (cont.)

Since the function flowchart has been modified, the way of the function to be called will also be changed

Original main flowchart: Modified main flowchart:


Page 1 Page 1
Start Start

Read Read
n1, n2 , n3 n1, n2 , n3

AVRG (result, n1, n2,n3) result =AVRG (n1, n2, n3)

Now, result is not


anymore a parameter
of the function-call
Print Print
result result

Stop Stop

Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 17
Control Structure

• Describe the flow of execution.


• In flowcharts, flow of execution is represented by
the arrow line.

• Types of control structure:


•Sequential
•Selection
•Repetition

Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 18
Sequential Structure

Multiple statements considered as one statement

A statement means
a command or an instruction
statement

 statement

statement

Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 19
Selection Structure
If
“do it or don’t” (one-choice)

TRUE
condition
condition

FALSE

statement
 statement

If set condition is true, execute the statement, else do


nothing

Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 20
Selection Structure (cont..)
If-else
“do this one or (two-choices)
the other one”

TRUE FALSE
condition
condition

Statement 1 Statement 2  statement

°
If set condition is true, execute the first statement, else
execute second statement

Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 21
Selection Structure (cont..)
Nested if
(if within if)

FALSE
test1 FALSE
test1
TRUE
TRUE
FALSE

TRUE
test2
°  °
statement

Considered as
one statement ° °
it is an “one-choice” if

Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 22
Selection Structure (cont..)

Complex if-else & if Statements

FALSE
condition
TRUE

statement

statement TRUE
condition

FALSE
statement

°
° Considered as one statement

Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 23
Repetition Structure
while Loop

It is a pre-test loop

°
FALSE
condition
conditio
n


TRUE
statement
body of loop

While a set condition is true, repeat statement (body of loop)

Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 24
Repetition Structure (cont…)
do-while Loop

It is a post-test loop

°
statement
 statement

condition
TRUE
FALSE

Do the statement (body of loop) while a condition is true

Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 25
Repetition Control Structure (cont…)

for Loop

It is a pre-test loop

initialization

° FALSE
condition

TRUE
body of loop

increment

Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 26
Example:
Start

Read
Length,
Input:
Width Length <- 5
Width <- 3

Calculate Area Process:


Area=Length * Width Area = 5 * 3 = 15

Calculate Perimeter Process:


Perimeter= Perimeter =
2 * (Width+Length) 2* (5+3) = 16
Output

Area: 15
Print
Area, Perimeter: 16
Perimeter

Stop

Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 27
Example:
What is the output of the following flowchart when the input Num= 10

Start
Enter a Number >> 10

Category A
Input:
Read Num
Num <- 10
Num = 10
10 > 0 ? => YES

Num>0? No

Print
"Category B"
Yes

Print
Output:
"Category A"
“Category A”

Stop

Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 28
Example:
What is the output of the following flowchart when the input is Num= 0

Start
Enter a Number >> 0

Category B
Read Num Input: Category A
Num <- 0
Num = 0 Output:
0 > 0 ? => NO “Category B”

Num>0? No

Print
"Category B"
Yes

Print
Output:
"Category A"
“Category A”

Stop

Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 29
Example:
What is the output of the following flowchart when the input is Num= 4

Start Variables
Variables
Variables(in
(in
(inmemory):
memory):
memory):

Num
Num
Num [[[ 444 ]]]
Read Num Input: Result
Result
Result [[[ 4
0710]
9 ]]] 0497 +++ 4312
Num <- 4 Count
Count [[[ 3
Count 420
1 ]]] 4312 --- 111

Initialize

Result=0
Count=Num

Enter a Number => 4


Count
Count
Count=
Count ===4
132
0
Print Count Count: 4
4
132>
>>>0
000?? ??=>
=>
=>YES
YES
YES
0 => YES
NO Count: 3
Count: 2
Result=Result + Count
Count>0? Yes Count: 1
Count=Count - 1
Count: 0
No
Result: 10

Print
Result

Stop

Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 30
Example:
What is the output of the following flowchart when the input is N = 6 10
Page 1 Page 2 5
average

Start
N=6
AVRG ( result,n1, n2,n3)

Read
N Sum = 10 + 5 + 6

sum = n1+ n2+n3

AVRG (average, 10, 5, N) average =


21/3

result = sum/3

Print
average Output:
Average: 7
Return

Stop

Introduction To Flowcharting | SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: July 2005 Slide 31

S-ar putea să vă placă și