SlideShare a Scribd company logo
ALGORITHMS AND
FLOWCHARTS
ALGORITHMS AND FLOWCHARTS
 A typical programming task can be divided into
two phases:
 Problem solving phase
 produce an ordered sequence of steps that describe
solution of problem
 this sequence of steps is called an algorithm
 Implementation phase
 implement the program in some programming
language
Steps in Problem Solving
 First produce a general algorithm (one can use
pseudocode)
 Refine the algorithm successively to get step by
step detailed algorithm that is very close to a
computer language.
 Pseudocode is an artificial and informal
language that helps programmers develop
algorithms. Pseudocode is very similar to
everyday English.
Pseudocode & Algorithm
 Example 1: Write an algorithm to
determine a student’s final grade and
indicate whether it is passing or failing.
The final grade is calculated as the
average of four marks.
Pseudocode & Algorithm
Pseudocode:
 Input a set of 4 marks
 Calculate their average by summing and dividing
by 4
 if average is below 50
Print “FAIL”
else
Print “PASS”
Pseudocode & Algorithm
 Detailed Algorithm
 Step 1: Input M1,M2,M3,M4
Step 2: GRADE  (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
endif
The Flowchart
 (Dictionary) A schematic representation of a sequence of
operations, as in a manufacturing process or computer
program.
 (Technical) A graphical representation of the sequence
of operations in an information system or program.
Information system flowcharts show how data flows from
source documents through the computer to final
distribution to users. Program flowcharts show the
sequence of instructions in a single program or
subroutine. Different symbols are used to draw each
type of flowchart.
The Flowchart
A Flowchart
shows logic of an algorithm
emphasizes individual steps and their
interconnections
e.g. control flow from one action to the next
Flowchart Symbols
Basic
Oval
Parallelogram
Rectangle
Diamond
Hybrid
Name Symbol Use in Flowchart
Denotes the beginning or end of the program
Denotes an input operation
Denotes an output operation
Denotes a decision (or branch) to be made.
The program should continue along one of
two routes. (e.g. IF/THEN/ELSE)
Denotes a process to be carried out
e.g. addition, subtraction, division etc.
Flow line Denotes the direction of logic flow in the program
Example
PRINT
“PASS”
Step 1: Input M1,M2,M3,M4
Step 2: GRADE  (M1+M2+M3+M4)/4
Step 3: if (GRADE <50) then
Print “FAIL”
else
Print “PASS”
endif
START
Input
M1,M2,M3,M4
GRADE(M1+M2+M3+M4)/4
IS
GRADE<5
0
PRINT
“FAIL”
STOP
YN
Example 2
 Write an algorithm and draw a flowchart to
convert the length in feet to centimeter.
Pseudocode:
 Input the length in feet (Lft)
 Calculate the length in cm (Lcm) by
multiplying LFT with 30
 Print length in cm (LCM)
Example 2
Algorithm
 Step 1: Input Lft
 Step 2: Lcm  Lft x 30
 Step 3: Print Lcm
START
Input
Lft
Lcm  Lft x 30
Print
Lcm
STOP
Flowchart
Example 3
Write an algorithm and draw a flowchart that
will read the two sides of a rectangle and
calculate its area.
Pseudocode
 Input the width (W) and Length (L) of a rectangle
 Calculate the area (A) by multiplying L with W
 Print A
Example 3
Algorithm
 Step 1: Input W,L
 Step 2: A  L x W
 Step 3: Print A
START
Input
W, L
A  L x W
Print
A
STOP
Example 4
 Write an algorithm and draw a flowchart that
will calculate the roots of a quadratic equation
 Hint: d = sqrt ( ), and the roots are:
x1 = (–b + d)/2a and x2 = (–b – d)/2a
2
0ax bx c  
2
4b ac
Example 4
Pseudocode:
 Input the coefficients (a, b, c) of the
quadratic equation
 Calculate d
 Calculate x1
 Calculate x2
 Print x1 and x2
Example 4
 Algorithm:
 Step 1: Input a, b, c
 Step 2: d  sqrt ( )
 Step 3: x1  (–b + d) / (2 x a)
 Step 4: x2  (–b – d) / (2 x a)
 Step 5: Print x1, x2
START
Input
a, b, c
d  sqrt(b x b – 4 x a x c)
Print
x1 ,x2
STOP
x1 (–b + d) / (2 x a)
X2  (–b – d) / (2 x a)
4b b a c   
DECISION STRUCTURES
 The expression A>B is a logical expression
 it describes a condition we want to test
 if A>B is true (if A is greater than B) we take
the action on left
 print the value of A
 if A>B is false (if A is not greater than B) we
take the action on right
 print the value of B
DECISION STRUCTURES
is
A>B
Print
B
Print
A
Y N
IF–THEN–ELSE STRUCTURE
 The structure is as follows
If condition then
true alternative
else
false alternative
endif
IF–THEN–ELSE STRUCTURE
 The algorithm for the flowchart is as
follows:
If A>B then
print A
else
print B
endif
is
A>B
Print
B
Print
A
Y N
Relational Operators
Relational Operators
Operator Description
> Greater than
< Less than
= Equal to
 Greater than or equal to
 Less than or equal to
 Not equal to
Example 5
 Write an algorithm that reads two values, determines the
largest value and prints the largest value with an
identifying message.
ALGORITHM
Step 1: Input VALUE1, VALUE2
Step 2: if (VALUE1 > VALUE2) then
MAX  VALUE1
else
MAX  VALUE2
endif
Step 3: Print “The largest value is”, MAX
Example 5
MAX  VALUE1
Print
“The largest value is”,
MAX
STOP
Y N
START
Input
VALUE1,VALUE2
MAX  VALUE2
is
VALUE1>VALUE2
NESTED IFS
 One of the alternatives within an IF–
THEN–ELSE statement
may involve further IF–THEN–ELSE
statement
Example 6
 Write an algorithm that reads three
numbers and prints the value of the largest
number.
Example 6
A Step 1: Input N1, N2, N3
B Step 2: if (N1>N2) then
c if (N1>N3) then
d MAX  N1 [N1>N2, N1>N3]
e else
f MAX  N3 [N3>N1>N2]
g endif
h else
i if (N2>N3) then
j MAX  N2 [N2>N1, N2>N3]
k else
l MAX  N3 [N3>N2>N1]
m endif
n endif
O Step 3: Print “The largest number is”, MAX
Example 6
 Flowchart: Draw the flowchart of the
above Algorithm.
Example 7
 Write and algorithm and draw a flowchart
to
a) read an employee name (NAME),
overtime hours worked (OVERTIME),
hours absent (ABSENT) and
b) determine the bonus payment
(PAYMENT).
Example 7
Bonus Schedule
OVERTIME – (2/3)*ABSENT Bonus Paid
>40 hours
>30 but  40 hours
>20 but  30 hours
>10 but  20 hours
 10 hours
$50
$40
$30
$20
$10
Step 1: Input NAME,OVERTIME,ABSENT
Step 2: if (OVERTIME–(2/3)*ABSENT > 40) then
PAYMENT  50
else if (OVERTIME–(2/3)*ABSENT > 30) then
PAYMENT  40
else if (OVERTIME–(2/3)*ABSENT > 20) then
PAYMENT  30
else if (OVERTIME–(2/3)*ABSENT > 10) then
PAYMENT 20
else
PAYMENT  10
endif
Step 3: Print “Bonus for”, NAME “is $”, PAYMENT
Example 7
 Flowchart: Draw the flowchart of the
above algorithm?

More Related Content

What's hot (20)

PPTX
Lexical analyzer
Farzana Aktar
 
PDF
Mathematical Logic
Joey Fontanilla Valdriz
 
PPTX
BOOLEAN ALGEBRA AND LOGIC GATE
Tamim Tanvir
 
PPTX
Evaluating Functions
NICO MONTERO
 
PPT
Infix prefix postfix
Self-Employed
 
PPTX
Simple annuities
Krysten Amoranto
 
PPT
Stacks
sweta dargad
 
PDF
BOOTH Algorithm for Multiplication
SnehalataAgasti
 
PPT
Bnf and ambiquity
BBDITM LUCKNOW
 
PDF
Discrete Structures. Lecture 1
Ali Usman
 
PPT
History of Computer
Muhammad Hammad Waseem
 
PPT
Basic structure of computers
Kumar
 
PPTX
Truth table
Liwayway Memije-Cruz
 
PPTX
General Mathematics - Representation and Types of Functions
Juan Miguel Palero
 
PPTX
Introduction to algorithms
Madishetty Prathibha
 
PPTX
Pseudocode-Flowchart
lotlot
 
PPTX
algorithm and flowchart computer science
aishvaryatamilarasoo
 
PPTX
Evolution of computer
PratikshaKhedkar1
 
PPTX
Evaluating functions basic rules
julienorman80065
 
PPTX
Chapter 5 boolean algebra
Praveen M Jigajinni
 
Lexical analyzer
Farzana Aktar
 
Mathematical Logic
Joey Fontanilla Valdriz
 
BOOLEAN ALGEBRA AND LOGIC GATE
Tamim Tanvir
 
Evaluating Functions
NICO MONTERO
 
Infix prefix postfix
Self-Employed
 
Simple annuities
Krysten Amoranto
 
Stacks
sweta dargad
 
BOOTH Algorithm for Multiplication
SnehalataAgasti
 
Bnf and ambiquity
BBDITM LUCKNOW
 
Discrete Structures. Lecture 1
Ali Usman
 
History of Computer
Muhammad Hammad Waseem
 
Basic structure of computers
Kumar
 
General Mathematics - Representation and Types of Functions
Juan Miguel Palero
 
Introduction to algorithms
Madishetty Prathibha
 
Pseudocode-Flowchart
lotlot
 
algorithm and flowchart computer science
aishvaryatamilarasoo
 
Evolution of computer
PratikshaKhedkar1
 
Evaluating functions basic rules
julienorman80065
 
Chapter 5 boolean algebra
Praveen M Jigajinni
 

Similar to Algorithmsandflowcharts1 (20)

PPT
01 Algorithms And Flowcharts.ppt
FerdieBalang
 
PPT
256958.ppt
Bimlesh7
 
PPT
Algorithmsandflowcharts1
ወዲ ህዝቢ
 
PPT
Algorithmsandflowcharts1
rajnidhiman
 
PPTX
Draw the flowchart of the above algorithm.pptx
Carlos701746
 
PPTX
Algorithms-Flowcharts for programming fundamental
fazayn927
 
PPTX
Algorithms and flowcharts
Samuel Igbanogu
 
PPTX
Problem Solving - Introduction to Flowcharts.pptx
aroojtmalik
 
PPT
Algorithms and flowcharts ppt (seminar presentation)..
Nagendra N
 
PDF
ALGORITHMS AND FLOWCHARTS
Kate Campbell
 
PPT
Algorithmsandflowcharts1
Jesuraj Love
 
PPT
Algorithmsandflowcharts1
luhkahreth
 
PPT
Algorithms and flowcharts1
Lincoln School
 
PPT
Algorithms and Flowchart usages in C laguage
BalaKrishnan466
 
PPT
Algorithms and Flowchart for IGCSE Students
MKKhaing
 
PDF
1153 algorithms%20and%20flowcharts
Dani Garnida
 
PPTX
Programming fundamentals lecture 4
Raja Hamid
 
PPT
Basic Slides on Algorithms and Flowcharts
moazwinner
 
PPT
Lecture1-Algorithms-and-Flowchart-ppt.ppt
samreen82
 
PPT
Lect1-Algorithms-and-Flowchart-ppt (1).ppt
olisahchristopher
 
01 Algorithms And Flowcharts.ppt
FerdieBalang
 
256958.ppt
Bimlesh7
 
Algorithmsandflowcharts1
ወዲ ህዝቢ
 
Algorithmsandflowcharts1
rajnidhiman
 
Draw the flowchart of the above algorithm.pptx
Carlos701746
 
Algorithms-Flowcharts for programming fundamental
fazayn927
 
Algorithms and flowcharts
Samuel Igbanogu
 
Problem Solving - Introduction to Flowcharts.pptx
aroojtmalik
 
Algorithms and flowcharts ppt (seminar presentation)..
Nagendra N
 
ALGORITHMS AND FLOWCHARTS
Kate Campbell
 
Algorithmsandflowcharts1
Jesuraj Love
 
Algorithmsandflowcharts1
luhkahreth
 
Algorithms and flowcharts1
Lincoln School
 
Algorithms and Flowchart usages in C laguage
BalaKrishnan466
 
Algorithms and Flowchart for IGCSE Students
MKKhaing
 
1153 algorithms%20and%20flowcharts
Dani Garnida
 
Programming fundamentals lecture 4
Raja Hamid
 
Basic Slides on Algorithms and Flowcharts
moazwinner
 
Lecture1-Algorithms-and-Flowchart-ppt.ppt
samreen82
 
Lect1-Algorithms-and-Flowchart-ppt (1).ppt
olisahchristopher
 
Ad

More from Emmanuel Alimpolos (20)

PPT
Amortization
Emmanuel Alimpolos
 
PPTX
Presentationonmemo 131008143853-phpapp02
Emmanuel Alimpolos
 
PPTX
Unit 1-120510090718-phpapp01
Emmanuel Alimpolos
 
PPT
Recruitment selection-1230614550740619-2
Emmanuel Alimpolos
 
PPTX
Pagbabagongmorpoponemiko 160907063021
Emmanuel Alimpolos
 
PPTX
Karlvinreportpresentationsafilipino 161108134808
Emmanuel Alimpolos
 
PPT
Howtowriteamemo 090920105907-phpapp02
Emmanuel Alimpolos
 
PPTX
Applyingforajob 120613221830-phpapp01
Emmanuel Alimpolos
 
PPTX
01microsoftofficeword2007introductionandparts 130906003510-
Emmanuel Alimpolos
 
PPT
2 2amortization-110921085439-phpapp01
Emmanuel Alimpolos
 
PPT
2 3sinkingfunds-110921085502-phpapp02
Emmanuel Alimpolos
 
PPTX
Probability
Emmanuel Alimpolos
 
PPTX
Midterm
Emmanuel Alimpolos
 
PDF
J introtojava1-pdf
Emmanuel Alimpolos
 
PDF
Java basic operators
Emmanuel Alimpolos
 
PPTX
Energy, work, power
Emmanuel Alimpolos
 
PDF
Java basic operators
Emmanuel Alimpolos
 
PPTX
statistic midterm
Emmanuel Alimpolos
 
PPSX
Coherentwriting 121002082424-phpapp01
Emmanuel Alimpolos
 
PPT
Batayangkaalamansapagsulat 140811070400-phpapp01
Emmanuel Alimpolos
 
Amortization
Emmanuel Alimpolos
 
Presentationonmemo 131008143853-phpapp02
Emmanuel Alimpolos
 
Unit 1-120510090718-phpapp01
Emmanuel Alimpolos
 
Recruitment selection-1230614550740619-2
Emmanuel Alimpolos
 
Pagbabagongmorpoponemiko 160907063021
Emmanuel Alimpolos
 
Karlvinreportpresentationsafilipino 161108134808
Emmanuel Alimpolos
 
Howtowriteamemo 090920105907-phpapp02
Emmanuel Alimpolos
 
Applyingforajob 120613221830-phpapp01
Emmanuel Alimpolos
 
01microsoftofficeword2007introductionandparts 130906003510-
Emmanuel Alimpolos
 
2 2amortization-110921085439-phpapp01
Emmanuel Alimpolos
 
2 3sinkingfunds-110921085502-phpapp02
Emmanuel Alimpolos
 
Probability
Emmanuel Alimpolos
 
J introtojava1-pdf
Emmanuel Alimpolos
 
Java basic operators
Emmanuel Alimpolos
 
Energy, work, power
Emmanuel Alimpolos
 
Java basic operators
Emmanuel Alimpolos
 
statistic midterm
Emmanuel Alimpolos
 
Coherentwriting 121002082424-phpapp01
Emmanuel Alimpolos
 
Batayangkaalamansapagsulat 140811070400-phpapp01
Emmanuel Alimpolos
 
Ad

Recently uploaded (20)

PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PDF
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PPTX
BANDHA (BANDAGES) PPT.pptx ayurveda shalya tantra
rakhan78619
 
PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PDF
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
PDF
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PPSX
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
PPTX
THE TAME BIRD AND THE FREE BIRD.pptxxxxx
MarcChristianNicolas
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PPTX
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
BANDHA (BANDAGES) PPT.pptx ayurveda shalya tantra
rakhan78619
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
SSHS-2025-PKLP_Quarter-1-Dr.-Kerby-Alvarez.pdf
AishahSangcopan1
 
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
THE TAME BIRD AND THE FREE BIRD.pptxxxxx
MarcChristianNicolas
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 

Algorithmsandflowcharts1

  • 2. ALGORITHMS AND FLOWCHARTS  A typical programming task can be divided into two phases:  Problem solving phase  produce an ordered sequence of steps that describe solution of problem  this sequence of steps is called an algorithm  Implementation phase  implement the program in some programming language
  • 3. Steps in Problem Solving  First produce a general algorithm (one can use pseudocode)  Refine the algorithm successively to get step by step detailed algorithm that is very close to a computer language.  Pseudocode is an artificial and informal language that helps programmers develop algorithms. Pseudocode is very similar to everyday English.
  • 4. Pseudocode & Algorithm  Example 1: Write an algorithm to determine a student’s final grade and indicate whether it is passing or failing. The final grade is calculated as the average of four marks.
  • 5. Pseudocode & Algorithm Pseudocode:  Input a set of 4 marks  Calculate their average by summing and dividing by 4  if average is below 50 Print “FAIL” else Print “PASS”
  • 6. Pseudocode & Algorithm  Detailed Algorithm  Step 1: Input M1,M2,M3,M4 Step 2: GRADE  (M1+M2+M3+M4)/4 Step 3: if (GRADE < 50) then Print “FAIL” else Print “PASS” endif
  • 7. The Flowchart  (Dictionary) A schematic representation of a sequence of operations, as in a manufacturing process or computer program.  (Technical) A graphical representation of the sequence of operations in an information system or program. Information system flowcharts show how data flows from source documents through the computer to final distribution to users. Program flowcharts show the sequence of instructions in a single program or subroutine. Different symbols are used to draw each type of flowchart.
  • 8. The Flowchart A Flowchart shows logic of an algorithm emphasizes individual steps and their interconnections e.g. control flow from one action to the next
  • 9. Flowchart Symbols Basic Oval Parallelogram Rectangle Diamond Hybrid Name Symbol Use in Flowchart Denotes the beginning or end of the program Denotes an input operation Denotes an output operation Denotes a decision (or branch) to be made. The program should continue along one of two routes. (e.g. IF/THEN/ELSE) Denotes a process to be carried out e.g. addition, subtraction, division etc. Flow line Denotes the direction of logic flow in the program
  • 10. Example PRINT “PASS” Step 1: Input M1,M2,M3,M4 Step 2: GRADE  (M1+M2+M3+M4)/4 Step 3: if (GRADE <50) then Print “FAIL” else Print “PASS” endif START Input M1,M2,M3,M4 GRADE(M1+M2+M3+M4)/4 IS GRADE<5 0 PRINT “FAIL” STOP YN
  • 11. Example 2  Write an algorithm and draw a flowchart to convert the length in feet to centimeter. Pseudocode:  Input the length in feet (Lft)  Calculate the length in cm (Lcm) by multiplying LFT with 30  Print length in cm (LCM)
  • 12. Example 2 Algorithm  Step 1: Input Lft  Step 2: Lcm  Lft x 30  Step 3: Print Lcm START Input Lft Lcm  Lft x 30 Print Lcm STOP Flowchart
  • 13. Example 3 Write an algorithm and draw a flowchart that will read the two sides of a rectangle and calculate its area. Pseudocode  Input the width (W) and Length (L) of a rectangle  Calculate the area (A) by multiplying L with W  Print A
  • 14. Example 3 Algorithm  Step 1: Input W,L  Step 2: A  L x W  Step 3: Print A START Input W, L A  L x W Print A STOP
  • 15. Example 4  Write an algorithm and draw a flowchart that will calculate the roots of a quadratic equation  Hint: d = sqrt ( ), and the roots are: x1 = (–b + d)/2a and x2 = (–b – d)/2a 2 0ax bx c   2 4b ac
  • 16. Example 4 Pseudocode:  Input the coefficients (a, b, c) of the quadratic equation  Calculate d  Calculate x1  Calculate x2  Print x1 and x2
  • 17. Example 4  Algorithm:  Step 1: Input a, b, c  Step 2: d  sqrt ( )  Step 3: x1  (–b + d) / (2 x a)  Step 4: x2  (–b – d) / (2 x a)  Step 5: Print x1, x2 START Input a, b, c d  sqrt(b x b – 4 x a x c) Print x1 ,x2 STOP x1 (–b + d) / (2 x a) X2  (–b – d) / (2 x a) 4b b a c   
  • 18. DECISION STRUCTURES  The expression A>B is a logical expression  it describes a condition we want to test  if A>B is true (if A is greater than B) we take the action on left  print the value of A  if A>B is false (if A is not greater than B) we take the action on right  print the value of B
  • 20. IF–THEN–ELSE STRUCTURE  The structure is as follows If condition then true alternative else false alternative endif
  • 21. IF–THEN–ELSE STRUCTURE  The algorithm for the flowchart is as follows: If A>B then print A else print B endif is A>B Print B Print A Y N
  • 22. Relational Operators Relational Operators Operator Description > Greater than < Less than = Equal to  Greater than or equal to  Less than or equal to  Not equal to
  • 23. Example 5  Write an algorithm that reads two values, determines the largest value and prints the largest value with an identifying message. ALGORITHM Step 1: Input VALUE1, VALUE2 Step 2: if (VALUE1 > VALUE2) then MAX  VALUE1 else MAX  VALUE2 endif Step 3: Print “The largest value is”, MAX
  • 24. Example 5 MAX  VALUE1 Print “The largest value is”, MAX STOP Y N START Input VALUE1,VALUE2 MAX  VALUE2 is VALUE1>VALUE2
  • 25. NESTED IFS  One of the alternatives within an IF– THEN–ELSE statement may involve further IF–THEN–ELSE statement
  • 26. Example 6  Write an algorithm that reads three numbers and prints the value of the largest number.
  • 27. Example 6 A Step 1: Input N1, N2, N3 B Step 2: if (N1>N2) then c if (N1>N3) then d MAX  N1 [N1>N2, N1>N3] e else f MAX  N3 [N3>N1>N2] g endif h else i if (N2>N3) then j MAX  N2 [N2>N1, N2>N3] k else l MAX  N3 [N3>N2>N1] m endif n endif O Step 3: Print “The largest number is”, MAX
  • 28. Example 6  Flowchart: Draw the flowchart of the above Algorithm.
  • 29. Example 7  Write and algorithm and draw a flowchart to a) read an employee name (NAME), overtime hours worked (OVERTIME), hours absent (ABSENT) and b) determine the bonus payment (PAYMENT).
  • 30. Example 7 Bonus Schedule OVERTIME – (2/3)*ABSENT Bonus Paid >40 hours >30 but  40 hours >20 but  30 hours >10 but  20 hours  10 hours $50 $40 $30 $20 $10
  • 31. Step 1: Input NAME,OVERTIME,ABSENT Step 2: if (OVERTIME–(2/3)*ABSENT > 40) then PAYMENT  50 else if (OVERTIME–(2/3)*ABSENT > 30) then PAYMENT  40 else if (OVERTIME–(2/3)*ABSENT > 20) then PAYMENT  30 else if (OVERTIME–(2/3)*ABSENT > 10) then PAYMENT 20 else PAYMENT  10 endif Step 3: Print “Bonus for”, NAME “is $”, PAYMENT
  • 32. Example 7  Flowchart: Draw the flowchart of the above algorithm?