Вы находитесь на странице: 1из 30

INTRODUCTION TO

ALGORITHMS & PROGRAMMING


Decision Statements and
Repetition Structures
M.Sc. Ibrahim Mesecan
imesecan@epoka.edu.al
M.Sc. Florenc Skuka
fskuka@Epoka.edu.al

Ibrahim Mesecan
Florenc Skuka
Page 1

LESSON 3:
FLOWCHARTS - 2

Ibrahim Mesecan
Florenc Skuka
Page 2

You are not born successful


You become successful.
Anonymous
Success Is Not an Accident:
Change Your Choices;
Change Your Life
Tommy Newberry
Ibrahim Mesecan
Florenc Skuka
Page 3

FLOWCHARTS
Mainly, flowcharts flow on a line.
Start
Get Data
Calculate
Results
Show Results

Ibrahim Mesecan
Florenc Skuka
Page 4

End
Flowcharts

FLOWCHARTS
But sometimes, when processing some steps,
there can be different branching and
jumping from one path to another inside the
module.
As a result, every module
should have one entry (A)
A
and one output line (B)
Calculate
Results
B

Ibrahim Mesecan
Florenc Skuka
Page 5

Flowcharts

RELATIONAL AND LOGICAL


OPERATORS
Program flow comes from A, and
according to the conditions given in
logical expression, it continues either
from C or from B.

Logical
Expr.

Logical Expressions are built using:


Relational operators (<, <=, etc.)
Logical operators (AND, OR, etc.)
Ibrahim Mesecan
Florenc Skuka
Page 6

Flowcharts

True

B False

RELATIONAL AND LOGICAL


OPERATORS
There are six relational operators:
<
: less than
<= : less than or equal to
>
: greater than
>= : greater than or equal to
== : equal to
!= : not equal to

Ibrahim Mesecan
Florenc Skuka
Page 7

Relational operators are used to find the relation


between two variables.
Each of these relational operators takes two operands.
These two operands must both be of the same type.
Flowcharts

RELATIONAL AND LOGICAL


OPERATORS
Using these operators, logical expressions
are formed. Logical expressions produce
A
either True, or False result.
Logical
Expr.

Ibrahim Mesecan
Florenc Skuka
Page 8

True

E.g. if x is 5 and y is 6 then the following B False


expression produce the below stated
results:
x<=y will produce the result (True)
x>=y will produce the result (False)
x==y will produce the result (False)
x!=y will produce the result (True)
Flowcharts

RELATIONAL AND LOGICAL


OPERATORS
There are three logical operators :
! (Not) converts true to false, and false to true.
A && B ( Both A and B must be true)
A || B ( At least one must be true)
A

!A

A && B

A || B

False

False

True

False

False

False

True

True

False

True

True

False

False

False

True

True

True

False

True

True

They are used to merge several conditions


into a single new condition.
Ibrahim Mesecan
Florenc Skuka
Page 9

Flowcharts

RELATIONAL AND LOGICAL


OPERATORS
Ex 1: If the decision statements mentions that N
must be between 1 and 100: 1 <= N <= 100. Actually
there are two relational expressions here.
(1 <= N) && (N <= 100)
Ex 2: If x and y must be less than 0: x, y < 0.
(x<0) && (y<0)
x must be less than zero; y must be less than zero too

Ex 3: If one of them (x or y) must be less than 0,


(x<0) || (y<0)
Ibrahim Mesecan
Florenc Skuka
Page 10

Flowcharts

FLOWCHARTS - DECISION
Design an algorithm to determine the state of water
water_temp<=0 Solid
0 < water_temp<=100 Liquid
100<water_temp Gaseous

Start
Get Data
Determine
the state
Show State

End
Ibrahim Mesecan
Florenc Skuka
Page 11

Flowcharts

FLOWCHARTS - DECISION
Then the standard algorithm can be

Ibrahim Mesecan
Florenc Skuka
Page 12

Flowcharts

FLOWCHARTS - DECISION
We can also take advantage of the structures we
use. For example the following operations, circled
with red, can be discarded if the decision structure
is changed

Ibrahim Mesecan
Florenc Skuka
Page 13

Flowcharts

FLOWCHARTS - DECISION
To simplify the case, lets have a
look how it would work if we just
had two conditions.
temp<=0 Ice
0<temp<100 Liquid

Ibrahim Mesecan
Florenc Skuka
Page 14

Flowcharts

FLOWCHARTS - DECISION
To simplify the case, lets have a
look how it would work if we just
had two conditions.
temp<=0 Solid
0<temp<100 Liquid

Ibrahim Mesecan
Florenc Skuka
Page 15

After the first decision (temp<=0), if the path down (false)


is followed, the second decision statement automatically
contains the meaning temp>0. By just adding the second
decision with the logical expression temp<100, the entire
logical expression becomes: if (0<temp<100) Liquid
This type of usage of decisions is
called Nested Decision statements.
Flowcharts

FLOWCHARTS - DECISION
Now, if the third logical check is
added to the false path of the
second decision, automatically
can be concluded that (temp is
not less than 0 and temp is not
less than 100) temp>=100.
As it can be seen from the
schema, the True paths are
connected to after the last
conclusion (point B). That
means the second expression is
nested in the first decision
structure.
Ibrahim Mesecan
Florenc Skuka
Page 16

Flowcharts

B
Nested
Structure

FLOWCHARTS- DECISION
Determine the letter grade of a student.
0..59
60..69
70..79
80..89
90..100

F
D
C
B
A

Ibrahim Mesecan
Florenc Skuka
Page 17

Flowcharts

LOOPS
Sometimes, we need to repeat some operations several
times. In such cases, we can use loops.
Loops have
One entry point (A)
One quit point (B)
Body of the loop and
A decision statement.

There are two types of loops.

Pretest Loop

Body of
the loop
B

B
Ibrahim Mesecan
Florenc Skuka
Page 18

Continue with
other processes

Posttest Loop
Flowcharts

LOOPS
There are two essential points in the
loop,
the decision statement that decides
whether to continue or not.
The loop control variable
The loop control variable (LCV) has to
be changed somewhere in the loop
And, while the condition is satisfied
the process is repeated.
If never changes, the loop continues
infinitely. This error is called Infinite
Loop error.
Ibrahim Mesecan
Florenc Skuka
Page 19

Flowcharts

DETERMINING LOOPS
When developing algorithms, several
steps are written down.
At this level, the variables are named
The repeating body is identified
The process
The loop control variable (LCV) and how is
modified in the body
The loop quit condition

are identified.
B

Ibrahim Mesecan
Florenc Skuka
Page 20

Flowcharts

DETERMINING LOOPS
Ex. : Calculate the sum of the numbers from m to n;
Name the variables:

m (small), n (big), cnt (counter), sum


Initial values: m and n will be given by the user;
cnt=m; the initial value for the counter;
sum=0
The Process: Add the current value of cnt to sum
(sum=sum+cnt);
Change loop condition (cnt=cnt+1)
Continue while cnt<=n
( Quit when cnt>n )
Ibrahim Mesecan
Florenc Skuka
Page 21

Flowcharts

DETERMINING LOOPS
E.x. : Calculate the sum of the numbers from 21 to 24
Cnt= 21;
Sum = Sum + Cnt 21; Cnt = Cnt + 1 22
Check if Cnt >24
Sum = Sum + Cnt 43; Cnt = Cnt + 1 23
Check if Cnt >24
Sum = Sum + Cnt 66; Cnt = Cnt + 1 24
Check if Cnt >24
Sum = Sum + Cnt 90; Cnt = Cnt + 1 25
Check if Cnt >24 Yes Quit
Show Result (Sum is 90)
Ibrahim Mesecan
Florenc Skuka
Page 22

Flowcharts

DETERMINING LOOPS
The Algorithm

Ibrahim Mesecan
Florenc Skuka
Page 23

Flowcharts

DETERMINING LOOPS
Ex : Find out the max number given by user. Read the
numbers as far as the given numbers are greater than 0.
Name the variables: One variable to keep max: Max;
Another var. to get the number entered by the user: Num
Initial values: Num: Doesnt matter, Max: must be
initialized to 0; Max=0
Number Entered (Num)
Max
Continue while Num >0
3
3
( Quit when Num<=0 )
5
5

Ibrahim Mesecan
Florenc Skuka
Page 24

Flowcharts

Quit

DETERMINING LOOPS
Then the algorithm can be summarized:

Ibrahim Mesecan
Florenc Skuka
Page 25

Flowcharts

DETERMINING LOOPS
Then the algorithm can be summarized:
Initialize Max;
Start point for the loop

The Loop

Read the number: Num


Decide to quit
Check if Num is greater than Max
Assign Num to Max

The jump point (Out of ) loop


Show the result
Ibrahim Mesecan
Florenc Skuka
Page 26

Flowcharts

DETERMINING LOOPS
Ex.2: Calculate and show the Greatest Common Divisor
(GCD) of two numbers given.

Euclidean Algorithm:
There are two numbers: Big and Small,
While Small is different than zero:
calculate the remainder: Rem=Big % Small
Then assign Small to Big then assign Rem to
Small (Big=Small; Small=Rem)
When Small is zero, the Big is the GCD.
Ibrahim Mesecan
Florenc Skuka
Page 27

Flowcharts

DETERMINING LOOPS
Euclidean Algorithm
E.g. 1: Calculate the GCD of 24 and 18;
Rem = 24 % 18
6 Big = 18; Small= 6
Rem = 18 % 6
0 Big =6; Small =0

GCD
E.g. 2: Calculate the GCD of 26 and 19;
Rem = 26 % 19
7 Big = 19; Small= 7
Rem = 19 % 7
5 Big =7; Small =5
Rem = 7 % 5
2 Big =5; Small =2
Rem = 5 % 2
1 Big =2; Small =1
Rem = 2 % 1
0 Big =1; Small =0
Ibrahim Mesecan
Florenc Skuka
Page 28

GCD
Flowcharts

DETERMINING LOOPS
Euclidean Algorithm
Name the variables: Big, Small, and Rem
Initial values: Get the numbers: Big and Small
Continue while Small != 0
( Quit when Small==0 )
The Process:
Rem=Big % Small;
Big = Small; Small = Rem

Ibrahim Mesecan
Florenc Skuka
Page 29

Flowcharts

DETERMINING LOOPS
Euclidean Algorithm

Ibrahim Mesecan
Florenc Skuka
Page 30

Flowcharts

Вам также может понравиться