_Algorithm_and_flowchart
_Algorithm_and_flowchart
Flowcharting
Flowchart:
Represents an
algorithm in
graphical symbols
Flowchart Symbols
Yes
No
Stop
Connectors on the same page
Start
1 2
2
Connectors on a different page
Page 1 Page 2
Start
2
1
Stop
Yes 1
No
2
The detail of how the function works
is put in another flowchart.
Read
n1, n2 , n3
Body of a function is
AVRG (result, n1, n2,n3) the same with
normal flowchart
Stop
End terminal
must be a “Return”
Start
N=6
AVRG ( result,n1, n2,n3)
Read
N Sum = 10 + 5 +
result = sum/3
Print
average Output:
Average: 7
Return
Stop
Sequence
In a computer program or an algorithm,
sequence involves simple steps which are
to be executed one after the other.
The steps are executed in the same order in which they are written.
In a flowchart,
sequence is expressed as:
In pseudocode,
sequence is expressed
as:
process 1
process 2
…
…
process n
Sequence
An Example Using Sequence
Problem: Write a set of instructions that describe how to make a pot of tea.
Pseudocode Flowchart
BEGIN
fill a kettle with water
boil the water in the
kettle
put the tea leaves in the
pot
pour boiling water in the
pot
END
Example:
Start
Read
Length, Input:
Width Length <- 5
Width <- 3
Area: 15
Print
Area, Perimeter: 16
Perimeter
Stop
Selection is used in a computer program
or algorithm
to determine which particular step or set
of steps is to be executed
Binary Selection Binary Selection
1. IF condition THEN
process 1
ENDIF
2. IF condition THEN
process 1
ELSE
process 2
ENDIF
Selection
Binary (structure)
Binary Selection Binary Selection
1. IF condition THEN
process 1
ENDIF
2. IF condition THEN
process 1
ELSE
process 2
ENDIF
Selection
Binary (flowchart structure)
Note: In a flowchart it is most important to indicate
1. which path is to be followed when the condition is true, and
2. which path to follow when the condition is false.
Without these indications the flowchart is open to more than one interpretation.
Note: There are two acceptable ways to represent a decision in all of the
structures.
Either method is acceptable. For consistency, the method 1 is used throughout this documen
1. The condition is expressed as a 2. The condition is expressed as a
statement and the two possible question and the two possible
outcomes are indicated by outcomes are indicated by
• True • Yes
• False • No
Selection
Binary (examples)
Selection is used in a computer program or algorithm
to determine which particular step or set of steps is to be executed.
Binary Selection
Flowchart
Binary Selection
Pseudocode
CASEWHERE signal is
red : stop the
vehicle
amber : stop the
vehicle
green : proceed
through the intersection
OTHERWISE : proceed
with caution
ENDCASE
Pseudo Code
O Algorithms are commonly written
using Pseudo code.
O The term “Code” usually refers to as
programming language.
O Pseudo code is an English like
language that is similar to
programming language that is used
to write Algorithms.
Looping
1. Ask the user for the Centigrade
temperature
2. Store the value in box C
3. Calculate the corresponding
Fahrenheit temperature
4. Store it in box F
5. Print the values in boxes C and F,
Appropriately labelled
6. Go to step 1.
O When the Algorithm reaches step 6, it does
not stop but rather transfers control to step
1 to repeat the Algorithm again.
O A Loop is a set of instructions which can
be repeated. In this case. Step 1 to step 5
form a loop.
O The problem is there’s no condition under
which the loop can be stopped.
O How long will the loop go for ?
O How do we stop the Algorithm ?
The While loop
1. Ask the user for the WHILE C is not 0 DO
Centigrade
temperature
2. Store the value in box C Set F to 32+(9C/5)
3. If C = 0, then Stop. Print C and F,
4. Calculate the Prompt the user
corresponding
for the
Fahrenheit temperature
5. Store it in box F Centigrade
6. Print the values in temperature C
boxes C and F,
Appropriately labelled
END WHILE
7. Go to step 1.
STOP
The While loop
When a WHILE loop has the
following steps
O The condition is tested.
O If it is true the statements
in the loop are tested and
the process is repeated
from step 1.
O If the condition is false
then the WHILE loop
terminates/Ends with the
END WHILE statement.
And the program ends
with the STOP statement.
Repetition
Repetition allows for a portion of an algorithm or computer
program
to be done any number of times
dependent on some condition being met.
An occurrence of repetition is usually known as a loop.
Repetition
Repetition In flowcharting
pre-test repetition
In pseudocode, pre-test is expressed as:
repetition is expressed as:
Repetition: Post-Test
• A post-tested loop executes the body of the loop before testing the
termination condition.
• This construct is often referred to as an unguarded loop.
• The body of the loop is repeatedly executed until the termination condition is
true.
An important difference between a pre-test and post-test loop is that the
statements of a post-test loop are executed at least once even if the
condition is originally true, whereas the body of the pre-test loop may never be
executed if the termination condition is Repetition
originally true.
A close look at the representations of the two loop types makes this point apparent.
Repetition In a flowchart
post-test repetition
In pseudocode, post-test is expressed as:
repetition is expressed as:
REPEAT
process
UNTIL condition is true
Repetition Pre-test (example)
Pre-test Repetition
Flowchart
Pre-test
Repetition
Pseudocode
Post-test Repetition
Flowchart
Post-test Repetition
Pseudocode
REPEAT
beat the egg whites
UNTIL fluffy
For Loop
O The For construct is a simple For C = 0 to 100
statement which executes a
piece of code a set number of
times. Set F to
O It consists of
32+9C/5
1. The word FOR
2. The loop variable, (problem
Print (C,F)
in the example)
3. The word TO END FOR
4. The final value
STOP
5. To word DO
6. One or more lines of
instructions to be executed (
The body of the for loop)
7. The END FOR statement
For Loop
RESULT
C F
0 32.0
1 33.8
2 35.6
3 37.4
4 39.2
5 41.0
6 42.8
7 44.6
8 46.4
9 48.2
10 50.0
IF Then ELSE
statement
The IF then ELSE consists Prompt the user for
of
two numbers A & B
O The word IF
O A condition IF A>B THEN
O The word THEN PRINT (“A is bigger”)
O One or more statements
called the THEN PART
O The word ELSE ELSE
O One or more statements PRINT (“B is bigger”)
called the ELSE PART
END IF
O The word END IF
STOP
Interesting ?