Flow Charts
Flow Charts
explicitly states the steps leading to the Allows the flowchart to be drawn without
Connector
solution, which must be transmitted to intersecting, lines or without a reverse flow
flowchart or pseudo-code.
Flow Lines Shows direction of flow
• Flowchart:
-A flowchart provides a visual • Flowchart General Rules:
representation of the patterns the 1- All flow charts start with a terminal.
algorithm follows in order to show the 2- All boxes of the flowchart are connected
with Arrows (not lines).
logic of program.
3- Flowchart symbols have an entry point
-It is a diagram made up of boxes,
on the top of the symbol with no other entry
diamonds and other shapes, connected points.
by arrows. 4- The exit point for all flowchart symbols is
-Each shape represents a step in the on the bottom except for the decision
process, while the arrows show the order symbol.
in which they occur. 5- The decision symbol has two exit points;
-Flowcharting combines symbols and these can be on the sides or the bottom and
flow lines in order to show figuratively one side.
the operation of an algorithm. 6- A flowchart flows from top to bottom.
-Generally drawn in early stages of However, an upward flow can be shown as
formulating computer solutions. long as it does not exceed 3
-Facilitate communication between symbols.
programmers and business people. 7- Connectors are used to connect breaks in
the flowchart. Examples are:
-Helpful in understanding the logic of • From one page to another page.
complicated and lengthy problems. • From the bottom of the page to top of the same
page.
• An upward flow of more than 3 symbols.
8- Subroutines (predefined processes) have
their own and independent flowcharts.
9- All flowcharts end with a terminal.
1
• Design Structures: + Example (2) : Draw a flowchart that
-Sequence: One statement is executed will read the two sides of a rectangle
after another and calculate its area
-Selection/Decision: Statements can be
executed or skipped depending on START
whether a condition evaluates to True or
False Read width, length
-Repetition: Statements are executed
repeatedly until a condition evaluates to area = length * width
True or False
Print area
• Keywords:
-Input: Read / Get / Accept
STOP
-Initialize: Set / Init
-Declare: Declare
-Processing: Compute / Calculate / Set /
Increment 2] Selection Structure:
-Output: Print / Display / Show (A) IF-THEN Structure:
The IF-THEN statement allows us to
• Relational Operators: choose whether to execute a set of
Operator Description program statements based on a test.
> Greater than
< Less than If Statement
= Equal to (Single Selection)
>= Greater than or equal to
T
<= Less than or equal to
<> Not equal to
F
1] Sequence Structure:
+ Example (1) : Draw a flowchart to
convert the length in feet to
centimeter.
(B) IF-THEN-ELSE Structure:
START
If ... else Statement
(Double Selection)
Read LFeet
F T
LCm = LFeet * 30
Print LCm
STOP
2
+ Example (1) : An algorithm that prints + Example (4) : Calculate the employees
the larger of two numbers. pay at an hourly rate, and overtime pay
(over 40 hours) at 1.5 times the hourly
rate.
F if T
a>b
F if T
Print b Print a HOURS > 40
PAY = RATE *
PAY =
[ 40 + 1.5 *
RATE * HOURS
(HOURS - 40) ]
True False
Condition
Statement(s)
+ Example (3) : Student passes if the
average score of his four subjects 60 or
more.
START Nested False
Conditon
Statement(s) Statement(s)
score =(m1+m2+m3+m4)/4
Statement(s) Statement(s)
F if T
score < 60
Statement(s)
Print “Pass” Print “Fail”
Statement(s)
STOP
3
+ Example (1) : - A company has two
payment policies. Some employees Another solution for Example (2)
have fixed monthly salary, while some
are paid on an hourly basis. START
- A rate is fixed for one working hour.
- Employees paid on an hourly basis
who exceed 40 working hours get a Input
X, Y, Z
bonus for the extra hours (at 1.5 times
the hourly rate).
T F
If X>Y
F if PayType T
If X>Z If Y>Z
= “Hourly” T T
PAY = F F
F if T
SLARAY HOURS > 40
STOP
START
Input
X, Y, Z
If X>Z
T && F
X>Y
If Y>Z
F T
STOP
4
+ Example (3) : A program that provides weather prediction according to expected
temperature.
F F F F F Print
Temp <= 0 Temp <= 12 Temp <= 25 Temp <= 75 Temp <= 100
“Burning”
T T T T T
Case of
Variable
+ Example: A company has four different medical plans. Each plan is given a code
corresponding to the following: Plan 1 = F, Plan 2 = B, Plan 3 = K, Plan 4 = E. The
company pays all for Plan 1. The individual has to partly pay for the other plans.
The payroll deduction for Plan 2 = 4.65, for Plan 3 = 7.85, and for Plan 4 = 5.50. Any
other codes are considered errors. Write the algorithm for a module to determine
the payroll deduction.
Algorithm:
INSURANCE DEDUCTION
CASE OF MEDCODE Case of
= “F": MEDCODE
MEDDEDUCTION = 0
= ”B":
MEDDEDUCTION = 4.65
= “K": other-
= “F” = “B” = “K” = “E” wise
MEDDEDUCTION = 7 85
= ”E": MED DED MED DED MED DED MED DED
ERROR
MEDDEDUCTION = 5.50 =0 = 4.65 = 7.85 = 5.50
OTHERWISE
PROCESS ERROR
END-OF-CASE
EXIT
5
3] Repetition Structures:
• Advantages of Repetition
Structures: Test False
Expression
-One of the basic logical structures of
computer programming.
-Defining loops allows computers to True
perform particular tasks repeatedly.
Body of
-You can write one set of instructions While Loop
that operates on multiple separate sets
of data. Statement just
• The Repetition structure can be Below While
implemented using:
(A) WHILE Loop
(B) DO WHILE Loop
• How WHILE Loop Works?
(C) FOR Loop
-The while loop evaluates the condition
• Any program instruction that
expression.
repeats a statement or sequence of -If the condition expression is true,
statements a number of times is statements inside the body of while loop
called an iteration or a loop. are executed.
• The commands used to create -Then, the condition expression is
iterations or loops are all based on evaluated again. This process goes on
logical tests. until the condition expression is false.
• The statements that execute -When the condition expression is false,
within a loop are known as the loop while loop is terminated.
body.
• A counter is any numeric variable • Make sure that the condition is
you use to count the number of actually going to turn false at some
times an event has occurred. point in time.
• Every time you add 1 to a variable • If the condition is always true,
is called incrementing the variable. then you will end up in an infinite
loop.
(A) WHILE Loop:
-This type of conditional loop tests for + Problem: Calculate and report the
terminating condition at the beginning average grade for a class
of the loop. - Discussion: The average grade equals
-No action is performed at all if the first the sum of all grades divided by the
test causes the terminating condition to number of students
evaluate as false. - Input: Student grades
-Sentinel-controlled. (controlled by - Processing: Find the sum of the grades;
user’s input) count the number of students; calculate
average
- Output: Average grade
6
+ Example (2) : An algorithm that reads
START
the exam results for 10 students and
displays the numbers of passed and
Initialize Sum
failed students. A student passes if his
& Counter to 0 mark is greater than or equal to 60.
START
Are No
there more
Data ? Set passes = 0
failures = 0
Yes students = 0
Print passes,
Read grade
Add grade failures
STOP
to Sum
STOP
+ Example (1) : An algorithm to print No
grade >= 60
out each character typed at a keyboard
until the character ‘q’ is entered. Yes
No
Letter < > ‘q’
Yes
Print letter
Read letter
STOP
7
+ Example (2) cont. : An algorithm that + Example (3) : An algorithm that reads
reads the exam results for a number of the exam results for 10 students and
students and displays the numbers of displays the numbers of passed and
passed and failed students. A student failed students. A student passes if his
passes if his mark is greater than or mark is greater than or equal to 60. If
equal to 60. more than half of the students pass, the
success rate is accepted by the school.
START START
Read students
No
students < 10
Print passes,
No Yes failures
num < students
Yes
If passes > No
Print passes, students/2
Read grade
failures
Yes
STOP Print
Read grade “Accepted rate”
No
grade >= 60
Yes STOP
No
Increment passes Increment failures grade >= 60
Yes
Increment students
8
+ Example (4) : Get two numbers from • How DO WHILE Loop Works?
the user and display their quotient. -The codes inside the body of loop is
Make sure that the divisor number is executed at least once. Then, only the
not zero. condition expression is checked.
START
-If the condition expression is true, the
body of loop is executed. This process
continues until the condition expression
Read dividend,
becomes false.
divisor
-When the test expression is false,
DO WHILE loop is terminated.
No
divisor = 0 • DO WHILE loop has the
convenience in the following
Yes situations:
Print “Divisor quotient = dividend/
-The condition is not known or even
must be non-zero” divisor defined until inside the loop
-Loop should execute at least once
Print “Enter dividend Print quotient
-When you are asking a question, whose
and divisor” answer will determine if the loop is
repeated
Read dividend,
divisor STOP
• Just like with a WHILE loop, it is
possible to end up in an infinite loop,
(B) DO WHILE Loop: so it is very important to make sure
-This type of conditional loop tests for
the end condition will always be met
terminating condition at the end of the
eventually.
loop.
-Executes at least once before checking
+ Example (1) : An algorithm to print
the condition.
out each character typed at a keyboard
until the character ‘q’ is entered.
START
Body of
Loop
Read letter
True Test
Expression Print letter
False
Yes
letter < > ‘q’
Statement just
Below Loop No
STOP
9
+ Example (2) : An algorithm that
accepts readings of city temperatures
and displays the number of warm cities Initilization
(>24). The program stops when user statement
Update
enters a negative number. statement
START
Test True Body of
Expression for Loop
warmCities = 0
False
Read temp
Statement just
Below Loop
No
temp > 24
Yes
• The starting state section allows
Increment
warmCities you to either declare a variable and
give it a value or give a value to an
already existing variable.
Yes • The condition section tells the
temp >= 0
program that as long as the
No
conditional expression is true the
Print loop should continue to repeat itself.
warmCities • The increment/update section is
the easiest way for a for loop to
STOP handle changing of the variable. It is
possible to do things like x+1, x = x -
10, or even x = random ( 5 )
(C) FOR Loop:
-Used when the number of iterations is
• How FOR Loop Works?
1. The starting statement is executed only
known in advance.
once at the beginning.
-Uses an initialization of the variable as
2. Then, the condition expression is
a starting point.
evaluated.
-The condition depends on the value of
3. If the condition expression is false, FOR
the variable.
loop isterminated. But if it is true,
-The variable is incremented on each
statements inside body of FOR loop are
iteration until it reaches the required
executed and the increment expression is
value.
executed.
4. Again, the condition expression is
evaluated and this process repeats until
the condition expression is false.
10
+ Example (1) : An algorithm to + Example (3) : An algorithm to
calculate the sum of a set of numbers. calculate the salary for the hourly-paid
employees.
START
START
sum =0
Read numEmp
Read count
i=1
i=1
No
No i <= numEmp
i <= count
Yes Yes
STOP
sum=sum+num sal= hours * rate
Increment i
+ Example (2) : An algorithm to
calculate the average of a set of
numbers. • Which Loop to Use?
There is usually more than one way
START
to solve almost every programming
problem
sum = 0 -In case the program performs an
average=sum / i indefinite number of iterations, as long as
i =1
Wrong : as “i” will be a certain condition remains true: Use
bigger than “count” WHILE
Read count -In case the program performs an
indefinite number of iterations and does
not need a condition at the start: Use
No
i <= count DO..WHILE
-In case the program performs a specific
Yes average=sum / count number of iterations: Use FOR
Read num
Print average
sum=sum+num
Increment i STOP
11
#Exercise: • Put True (T) or False (F):
• Complete: 1. All boxes of the flowchart are connected
1. Each ............... represents a step in the with lines.
process, while the ............... show the order 2. The WHILE loop is used in case the
in which they occur. program performs an indefinite number of
2. The algorithms can be designed through iterations, as long as a certain condition
the use of ............... or ............... . remains true.
3. The ............... has two exit points; these 3. Nested IF is used in case of multiple
can be on the sides or the bottom and one embedded decisions.
side. 4. An algorithm can be defined as a
4. Selection structures are called ............... difference between a desired situation and
selection structures when there are two or current situation.
more constants to choose from. 5. The FOR loop is used in case the
5. If the condition is always true, then you program performs an indefinite number of
will end up in an ............... . iterations and does not need a condition at
6. The statements that execute within a the start.
loop are known as the ............... . 6. Every Nested IF can be represented
7. The ............... are used in case an upward using Case selection.
flow will traverse more than three
symbols.
8. In the ............... design structure, • Practice:
statements can be executed or skipped 1. Flowchart for program that provides
depending on whether a condition recommendations for hotels by
evaluates to True or False. interpreting the hotel stars ratings made
9. The ............... in the FOR loop handles by users. Each star rate represents a
changing the value of the variable. specificmeaning.
10.The ............... loop is used when processes 2. Flowchart for program that will output
should be executed at least once. the square of any number input until the
11. In flowchart, the ............... symbol is used number input is zero.
to ask a question that can be answered
with binary format.