0% found this document useful (0 votes)
27 views

ADS CH 2 - Flowcharts

The document discusses flowcharts, which are diagrams that represent algorithms visually using geometric shapes and arrows. It describes the common symbols used in flowcharts, including start/stop blocks, input/output blocks, processing blocks, subroutine blocks, conditional blocks, and more. It also covers principles for designing structured and readable flowcharts, such as using sequential, alternative, and repetitive control structures.

Uploaded by

Audina Mendes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

ADS CH 2 - Flowcharts

The document discusses flowcharts, which are diagrams that represent algorithms visually using geometric shapes and arrows. It describes the common symbols used in flowcharts, including start/stop blocks, input/output blocks, processing blocks, subroutine blocks, conditional blocks, and more. It also covers principles for designing structured and readable flowcharts, such as using sequential, alternative, and repetitive control structures.

Uploaded by

Audina Mendes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

1

Algorithms and
Data Structures

Course 2
Flowcharts

1 11/6/2017 9:18 PM
2 2

Flowcharts

 A flowchart is a type of diagram that describes an


algorithm, showing its steps and their order. This
graphical representation can give a step-by-step solution
to a given problem.
 A flowchart offers an expressive, synthetic and easy to
follow method of describing an algorithm, by viewing the
clear steps included in it. It shows how the algorithm is
working by using various graphical symbols.
 The steps of the algorithm are implemented using
shapes (blocks) of various kinds, and their order is
defined by connecting these blocks with arrows.
3 3

Flowchart building blocks (1)

 The symbols used in flowcharts are basically geometric


shapes. Each shape (block) has a certain functionality
assigned to it. Usually a block is associated to one or
more elementary operations and contains the operation(s)
to be performed at that stage.
 The blocks are linked by arrows which show the "flow of
control“ (control flow). The control flow refers to the order in
which the individual statements, instructions, or subroutine
calls are executed or evaluated. An arrow coming from one
block and ending at another block indicates that the control
passes to the block the arrow points to.
4 4

Flowchart building blocks (2)

 The most common types of blocks used in a flowchart


are:
– Start and Stop blocks – they indicate the beginning and
the end of the algorithm. They are represented as
rounded rectangles or ovals, usually containing the word
"START" or "STOP", or another phrase signaling the
start or end of the process.

START
STOP
5 5

Flowchart building blocks (3)

– Input (reading) and Output (writing) blocks – the input


block is used for reading the input data required by the
algorithm; the output block specifies the output data (the
results) that must be displayed. They are represented as
parallelograms, having one arrow coming in and one
arrow going out.

READ list of inputs WRITE list of outputs


6 6

Flowchart building blocks (4)

– Assignment (processing) block – it is a step of the


algorithm where different calculations are performed. It is
represented as a rectangle.
Variable = Expression
ex. X = X + 1

– Subroutine blocks – these are used to show complex


processing steps which may be detailed in a separate
flowchart. The subroutine block is represented as a
rectangle with double-struck vertical edges.
CALL subroutine
7 7

Flowchart building blocks (5)

– Conditional or decision block – it is represented as a


rhombus (diamond) showing where a decision is
necessary, commonly a Yes/No question or True/False
test. The conditional symbol is peculiar in that it has one
arrow coming in and two arrows going out of it (one
corresponding to Yes or True, and one corresponding to
No or False). The outgoing arrows should always be
labeled.

NO YES
condition
8 8

Flowchart building blocks (6)

– In a conditional block more than two outgoing arrows can


be used, but this is normally a clear indicator that a
complex decision is being taken, in which case it may
need to be broken-down further. This is usually the case
when you want to evaluate an expression that can take
many possible values.

expression
9 9

Flowchart building blocks (7)

 Conventionally, it is agreed that the blocks should be


written vertically, meaning that all processes should flow
from top to bottom. The arrows connecting the blocks
indicate the order of processing operations.
 In a flowchart, the arrows should not cross. To avoid this,
we have the following options:
– wherever two lines accidentally cross in the drawing, one
of them may be drawn with a small semicircle over the
other, showing that no junction is intended.
10 10

Flowchart building blocks (8)

– another way to avoid the arrows from crossing would be


by using labeled connectors. Connectors are used in
complex or multi-sheet diagrams to substitute for arrows.
They are used in pairs. We may have any number of
"inflow" connectors, but only one "outflow" connector
corresponding to them. The inflow connectors will be
placed where the flow is interrupted, while the outflow
connector will be inserted where the flow must continue.
The paired connectors must have the same label.
11 11

Flowchart building blocks (9)

There are two types of labeled connectors:


• in-page connectors – they are used if the interrupted
flow will continue inside the same page. They are
represented by circles containing a label.
a
a

• off-page connectors – they are used to specify that


the flow will continue on a different page. They are
represented by an identifying label inside a pentagon.
1
1
12 12

Flowchart building blocks (10)

– Junction symbol – generally, it is represented with a


black spot (blob), showing where multiple control flows
converge in a single exit flow. A junction symbol will
have more than one arrow coming into it, but only one
going out. These are useful to represent iterative or
alternative processes.

 In a flowchart other symbols can be used, but they must


be explained, and their meaning and functionality must
be clarified.
13 13

Principles of designing flowcharts

In designing a flowchart some rules must be obeyed:


– any flowchart begins with the Start block
– after processing a block, the next block will be executed
– after evaluating a decision block, the block corresponding
to the value of the condition will be executed. For a simple
decision block with two outgoing arrows, if the condition is
true, then the block connected to Yes will be processed;
otherwise, the block connected to No will be executed
– the flow ends with the Stop block
– the order of the blocks is not arbitrary, but it is
determined by the algorithm
14 14

Structured programming

 An algorithm (a flowchart) is structured if it meets the


following conditions:
– has only one entry point (contains a single Start block)
– has only one exit point (contains a single Stop block)
– contains only fundamental control structures, namely:
• sequential structures
• alternative structures
• repetitive structures
The structured programming theorem states that these
three ways of combining programs – sequencing, selection,
and iteration – are sufficient to express any algorithm.
15 15

Fundamental control structures (1)

 Sequential (linear) structure – it designates one or


more operations that are executed one after another, in a
linear sequence.

sequence 1 BEGIN
sequence 1;
sequence 2
sequence 2;
……………. ……..
sequence n;
sequence n END
16 16

Fundamental control structures (2)

 Alternative structures (selections) – in this case, one of


a number of sequences is executed depending on the
state of the program.
There are two types of alternative structures:
– selection: IF-THEN-ELSE structure, and its particular
cases IF-THEN and IF-ELSE
– multiple selection: CASE-OF structure (also known as
SWITCH structure)
17 17

Fundamental control structures (3)

– IF-THEN-ELSE structure – selects the action to be


executed depending on whether the specified condition
is true or false.

NO YES IF condition THEN


condition
sequence 1
ELSE
sequence 2 sequence 1
sequence 2
ENDIF
18 18

Fundamental control structures (4)

– IF-THEN and IF-ELSE structures – these are particular


cases of the IF-THEN-ELSE structure.

NO YES NO YES
condition condition

sequence 1 sequence 2

IF condition THEN IF NOT condition THEN


sequence 1 sequence 2
ENDIF ENDIF
19 19

Fundamental control structures (5)

– CASE-OF structure – it compares the value of an


expression with some specific values, and takes action
according to the first constant that matches.

CASE expression OF
expression
value 1 : sequence 1
v1
v2 vn value 2 : sequence 2
seq. 1 seq. 2 ……. seq. n def. seq. .......
value n : sequence n
ELSE: default sequence
ENDCASE
20 20

Fundamental control structures (6)

 Repetitive structures (iterations or loops) – in this case,


a sequence of statements (which is specified once), may
be carried out several times in succession.
There are different types of repetitive structures:
– condition-controlled loops – depending on the moment
the condition is evaluated we may have:
• pre-test loops: WHILE (or WHILE-DO) statement
• post-test loops: DO-UNTIL (or REPEAT-UNTIL) and
DO-WHILE structures
– counter-controlled loops: FOR statement
21 21

Fundamental control structures (7)

– WHILE loop – allows to repeatedly execute code as long


as a given boolean condition is true. The While loop
checks the condition before the block is executed.

WHILE condition
YES
condition sequence
ENDWHILE
NO sequence
22 22

Fundamental control structures (8)

– DO-WHILE and DO-UNTIL loops – the code will be


executed repeatedly as long as / until a given condition is
true. The condition is checked after the block is executed.

sequence sequence

YES NO
condition condition

NO YES
DO DO / REPEAT
sequence sequence
WHILE condition UNTIL condition
23 23

Fundamental control structures (9)

– FOR loop – used for repeating a sequence a certain


number of times. It uses a loop counter (or loop variable)
that controls the iterations of the loop.

v = vi FOR v = vi TO vf STEP vs
sequence
ENDFOR
YES
v ≤ vf where:
v – the loop variable (counter)
NO sequence
vi – the initial (starting) value
vf – the final (ending) value
v = v + vs
vs – the step value
24 24

Control structures equivalencies (1)

 Any algorithm can be written by using only these


fundamental control structures. They can be combined in
a sequential or nested manner. The concept of nesting
allows the mixing (or placing one inside another) of
different categories of control structures.
 Moreover, any algorithm can be transformed into a
structured form involving only IF-THEN-ELSE choices
and WHILE loops. This is possible by using nested
structures, duplicated code and/or additional Boolean
variables (true/false flags).
25 25

Control structures equivalencies (2)

– CASE-OF equivalence with IF-THEN-ELSE:


IF expression = value 1 THEN
sequence 1
CASE expression OF
ELSE
value 1 : sequence 1
IF expression = value 2 THEN
value 2 : sequence 2
sequence 2
....... ELSE
value n : sequence n .......
ELSE: default sequence IF expression = value n THEN
ENDCASE sequence n
ELSE
default sequence
ENDIF
.......
ENDIF
ENDIF
26 26

Control structures equivalencies (3)

– DO-WHILE and DO-UNTIL equivalencies with WHILE:


DO DO
sequence sequence
WHILE condition UNTIL condition
sequence sequence
WHILE condition WHILE NOT condition
sequence sequence
ENDWHILE ENDWHILE

– FOR equivalence with WHILE:


v = vi
FOR v = vi TO vf STEP vs WHILE v ≤ vf
sequence sequence
ENDFOR v = v + vs
ENDWHILE
27 27

The END

You might also like