SlideShare a Scribd company logo
1 of 44
A Beginner’s Guide to 
Programming Logic, 
Introductory 
Chapter 3 
Understanding Structure
Objectives 
In this chapter, you will learn about: 
• The features of unstructured spaghetti code 
• The three basic structures—sequence, selection, 
and loop 
• Using a priming input to structure a program 
• The need for structure 
• Recognizing structure 
• Structuring and modularizing unstructured logic 
A Beginner's Guide to Programming Logic, Introductory 2
Understanding Unstructured 
Spaghetti Code 
• Spaghetti code 
– Logically snarled program statements 
– Can be the result of poor program design 
– Programs often work but are difficult to read and 
maintain 
– Convoluted logic usually requires more code 
• Unstructured programs 
– Do not follow the rules of structured logic 
• Structured programs 
– Do follow rules of structured logic 
A Beginner's Guide to Programming Logic, Introductory 3
Figure 3-1 Spaghetti code logic for washing a dog 
A Beginner's Guide to Programming Logic, Introductory 4
Understanding the Three Basic 
Structures 
• Structure 
– Basic unit of programming logic 
– Sequence 
• Perform actions in order 
• No branching or skipping any task 
– Selection (decision) 
• Ask a question, take one of two actions 
• Dual-alternative or single-alternative ifs 
– Loop 
• Repeat actions based on answer to a question 
A Beginner's Guide to Programming Logic, Introductory 5
Understanding the Three Basic 
Structures (continued) 
Figure 3-2 Sequence structure 
A Beginner's Guide to Programming Logic, Introductory 6
Understanding the Three Basic 
Structures (continued) 
Figure 3-3 Selection structure 
A Beginner's Guide to Programming Logic, Introductory 7
Understanding the Three Basic 
Structures (continued) 
• Dual-alternative if 
– Contains two alternatives 
– If-then-else structure 
if someCondition is true then 
do oneProcess 
else 
do theOtherProcess 
A Beginner's Guide to Programming Logic, Introductory 8
Understanding the Three Basic 
Structures (continued) 
• Single-alternative if 
if employee belongs to dentalPlan then 
deduct $40 from employeeGrossPay 
– Else clause is not required 
• null case 
– Situation where nothing is done 
A Beginner's Guide to Programming Logic, Introductory 9
Understanding the Three Basic 
Structures (continued) 
Figure 3-4 Single-alternative selection structure 
A Beginner's Guide to Programming Logic, Introductory 10
Understanding the Three Basic 
Structures (continued) 
• Loop structure 
– Repeats a set of actions based on the answer to a 
question 
• Loop body 
– Also called repetition or iteration 
– Question is asked first in the most common form of 
loop 
– while … do or while loop 
A Beginner's Guide to Programming Logic, Introductory 11
Understanding the Three Basic 
Structures (continued) 
Figure 3-5 Loop structure 
A Beginner's Guide to Programming Logic, Introductory 12
Understanding the Three Basic 
Structures (continued) 
• Loop structure 
while testCondition continues to be true 
do someProcess 
while quantityInInventory remains low 
continue to orderItems 
A Beginner's Guide to Programming Logic, Introductory 13
Understanding the Three Basic 
Structures (continued) 
• All logic problems can be solved using only these 
three structures 
• Structures can be combined in an infinite number 
of ways 
• Stacking 
– Attaching structures end-to-end 
• End-structure statements 
– Indicate the end of a structure 
– The endif statement ends an if-then-else structure 
– The endwhile ends a loop structure 
A Beginner's Guide to Programming Logic, Introductory 14
Understanding the Three Basic 
Structures (continued) 
Figure 3-6 Structured flowchart and pseudocode with three stacked structures 
A Beginner's Guide to Programming Logic, Introductory 15
Understanding the Three Basic 
Structures (continued) 
• Any individual task or step in a structure can be 
replaced by a structure 
• Nesting 
– Placing one structure within another 
– Indent the nested structure’s statements 
• Block 
– Group of statements that execute as a single unit 
A Beginner's Guide to Programming Logic, Introductory 16
Understanding the Three Basic 
Structures (continued) 
Figure 3-7 Flowchart and pseudocode showing nested structures—a 
sequence nested within a selection 
A Beginner's Guide to Programming Logic, Introductory 17
Understanding the Three Basic 
Structures (continued) 
Figure 3-8 Flowchart and pseudocode showing nested structures—a loop 
nested within a sequence, nested within a selection 
A Beginner's Guide to Programming Logic, Introductory 18
Understanding the Three Basic 
Structures (continued) 
Figure 3-9 Flowchart and pseudocode for loop within selection within 
sequence within selection 
A Beginner's Guide to Programming Logic, Introductory 19
Understanding the Three Basic 
Structures (continued) 
• Structured programs have the following 
characteristics: 
– Include only combinations of the three basic structures 
– Each of the structures has a single entry point and a 
single exit point 
– Structures can be stacked or connected to one 
another only at their entry or exit points 
– Any structure can be nested within another structure 
A Beginner's Guide to Programming Logic, Introductory 20
Using a Priming Input to Structure 
a Program 
• Priming read (or priming input) 
– Reads the first input data record 
– Outside the loop that reads the rest of the records 
– Helps keep the program structured 
• Analyze a flowchart for structure one step at a time 
• Watch for unstructured loops that do not follow this 
order 
– First ask a question 
– Take action based on the answer 
– Return to ask the question again 
A Beginner's Guide to Programming Logic, Introductory 21
Using a Priming Input to Structure 
a Program (continued) 
Figure 3-15 Structured, but nonfunctional, flowchart of number-doubling 
problem 
A Beginner's Guide to Programming Logic, Introductory 22
Using a Priming Input to Structure 
a Program (continued) 
Figure 3-16 Functional but unstructured flowchart 
A Beginner's Guide to Programming Logic, Introductory 23
Using a Priming Input to Structure 
a Program (continued) 
• Priming read sets up the process so the loop can be 
structured 
• To analyze a flowchart’s structure, try writing 
pseudocode for it 
start 
get inputNumber 
while not eof 
calculatedAnswer = inputNumber * 2 
print calculatedAnswer 
get inputNumber 
endwhile 
stop 
A Beginner's Guide to Programming Logic, Introductory 24
Figure 3-17 Functional, structured flowchart and pseudocode for the number-doubling 
problem 
A Beginner's Guide to Programming Logic, Introductory 25
Figure 3-18 Structured but incorrect solution to the number-doubling problem 
A Beginner's Guide to Programming Logic, Introductory 26
Understanding the Reasons for 
Structure 
• Clarity 
• Professionalism 
• Efficiency 
• Ease of maintenance 
• Supports modularity 
A Beginner's Guide to Programming Logic, Introductory 27
Recognizing Structure 
• Any set of instructions can be expressed in 
structured format 
• Any task to which you can apply rules can be 
expressed logically using sequence, selection, loop 
• It can be difficult to detect whether a flowchart is 
structured 
A Beginner's Guide to Programming Logic, Introductory 28
Recognizing Structure (continued) 
Figure 3-20 Example 2 
A Beginner's Guide to Programming Logic, Introductory 29
Recognizing Structure (continued) 
Figure 3-21 Example 3 
A Beginner's Guide to Programming Logic, Introductory 30
Recognizing Structure (continued) 
• Single process like G is part of an acceptable 
structure 
– At least the beginning of a sequence structure 
Figure 3-22 Untangling Example 3, first step 
A Beginner's Guide to Programming Logic, Introductory 31
Recognizing Structure (continued) 
• H begins a selection structure 
– Sequences never have decisions in them 
– Logic never returns to G 
Figure 3-23 Untangling Example 3, second step 
A Beginner's Guide to Programming Logic, Introductory 32
Recognizing Structure (continued) 
• Pull up on the flowline from the left side of H 
Figure 3-24 Untangling Example 3, third step 
A Beginner's Guide to Programming Logic, Introductory 33
Recognizing Structure (continued) 
• Next, pull up the flowline on the right side of H 
Figure 3-25 Untangling Example 3, fourth step 
A Beginner's Guide to Programming Logic, Introductory 34
Recognizing Structure (continued) 
• Pull up the flowline on the left side of I and untangle 
it from the B selection by repeating J 
Figure 3-26 Untangling Example 3, fifth step 
A Beginner's Guide to Programming Logic, Introductory 35
Recognizing Structure (continued) 
• Now pull up the flowline on the right side of I 
Figure 3-27 Untangling Example 3, sixth step 
A Beginner's Guide to Programming Logic, Introductory 36
Recognizing Structure (continued) 
• Bring together the loose ends of I and of H 
Figure 3-28 Finished flowchart and pseudocode for untangling Example 3 
A Beginner's Guide to Programming Logic, Introductory 37
Structuring and Modularizing 
Unstructured Logic 
• Dog-washing process 
– Unstructured 
– Can be reconfigured to be structured 
• First step simple sequence 
Figure 3-29 Washing the dog, part 1 
A Beginner's Guide to Programming Logic, Introductory 38
Structuring and Modularizing 
Unstructured Logic (continued) 
• After the dog runs away 
– Catch the dog and determine whether he runs away 
again 
– A loop begins 
Figure 3-30 Washing the dog, part 2 
A Beginner's Guide to Programming Logic, Introductory 39
Figure 3-31 Washing the dog, part 3 
A Beginner's Guide to Programming Logic, Introductory 40
Figure 3-33 Structured dog-washing flowchart and pseudocode 
A Beginner's Guide to Programming Logic, Introductory 41
Figure 3-34 Modularized version of the dog-washing program 
A Beginner's Guide to Programming Logic, Introductory 42
Summary 
• Spaghetti code 
– Snarled program logic 
• Three basic structures 
– Sequence, selection, and loop 
– Combined by stacking and nesting 
• Priming read 
– Statement that reads the first input data record 
A Beginner's Guide to Programming Logic, Introductory 43
Summary (continued) 
• Structured techniques promote: 
– Clarity 
– Professionalism 
– Efficiency 
– Modularity 
• Flowchart can be made structured by untangling 
A Beginner's Guide to Programming Logic, Introductory 44

More Related Content

What's hot

Pressman ch-11-component-level-design
Pressman ch-11-component-level-designPressman ch-11-component-level-design
Pressman ch-11-component-level-design
Oliver Cheng
 
Architectural styles and patterns
Architectural styles and patternsArchitectural styles and patterns
Architectural styles and patterns
Himanshu
 

What's hot (20)

Software design
Software designSoftware design
Software design
 
A presentation on software crisis
A presentation on software crisisA presentation on software crisis
A presentation on software crisis
 
Unit 5- Architectural Design in software engineering
Unit 5- Architectural Design in software engineering Unit 5- Architectural Design in software engineering
Unit 5- Architectural Design in software engineering
 
Introduction to software engineering
Introduction to software engineeringIntroduction to software engineering
Introduction to software engineering
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
 
Chapter 01 software engineering pressman
Chapter 01  software engineering pressmanChapter 01  software engineering pressman
Chapter 01 software engineering pressman
 
Design engineering
Design engineeringDesign engineering
Design engineering
 
Best,worst,average case .17581556 045
Best,worst,average case .17581556 045Best,worst,average case .17581556 045
Best,worst,average case .17581556 045
 
Architecture design in software engineering
Architecture design in software engineeringArchitecture design in software engineering
Architecture design in software engineering
 
Introduction to programming
Introduction to programmingIntroduction to programming
Introduction to programming
 
Software reverse engineering
Software reverse engineeringSoftware reverse engineering
Software reverse engineering
 
Chapter 1 - An Introduction to Programming
Chapter 1 - An Introduction to ProgrammingChapter 1 - An Introduction to Programming
Chapter 1 - An Introduction to Programming
 
CIS110 Computer Programming Design Chapter (1)
CIS110 Computer Programming Design Chapter  (1)CIS110 Computer Programming Design Chapter  (1)
CIS110 Computer Programming Design Chapter (1)
 
Programming Fundamentals
Programming FundamentalsProgramming Fundamentals
Programming Fundamentals
 
Incremental model (software engineering)
Incremental model (software engineering)Incremental model (software engineering)
Incremental model (software engineering)
 
Pressman ch-11-component-level-design
Pressman ch-11-component-level-designPressman ch-11-component-level-design
Pressman ch-11-component-level-design
 
Software Re-Engineering
Software Re-EngineeringSoftware Re-Engineering
Software Re-Engineering
 
Software Architecture
Software ArchitectureSoftware Architecture
Software Architecture
 
Programing Slicing and Its applications
Programing Slicing and Its applicationsPrograming Slicing and Its applications
Programing Slicing and Its applications
 
Architectural styles and patterns
Architectural styles and patternsArchitectural styles and patterns
Architectural styles and patterns
 

Viewers also liked

Building the baseline
Building the baselineBuilding the baseline
Building the baseline
Carl Deala
 
Introduction to Programming
Introduction to ProgrammingIntroduction to Programming
Introduction to Programming
Chaffey College
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
paramalways
 
Minto pyramid
Minto pyramidMinto pyramid
Minto pyramid
shachihp
 

Viewers also liked (14)

Building the baseline
Building the baselineBuilding the baseline
Building the baseline
 
Logic Formulation 1
Logic Formulation 1Logic Formulation 1
Logic Formulation 1
 
Komunikasyon
KomunikasyonKomunikasyon
Komunikasyon
 
CIS110 Computer Programming Design Chapter (4)
CIS110 Computer Programming Design Chapter  (4)CIS110 Computer Programming Design Chapter  (4)
CIS110 Computer Programming Design Chapter (4)
 
Introduction to Programming
Introduction to ProgrammingIntroduction to Programming
Introduction to Programming
 
CIS110 Computer Programming Design Chapter (5)
CIS110 Computer Programming Design Chapter  (5)CIS110 Computer Programming Design Chapter  (5)
CIS110 Computer Programming Design Chapter (5)
 
Csharp4 basics
Csharp4 basicsCsharp4 basics
Csharp4 basics
 
Introduction to Using PHP & MVC Frameworks
Introduction to Using PHP & MVC FrameworksIntroduction to Using PHP & MVC Frameworks
Introduction to Using PHP & MVC Frameworks
 
Aralin sa Pakikinig
Aralin sa PakikinigAralin sa Pakikinig
Aralin sa Pakikinig
 
Mga Modelo ng Komunikasyon
Mga Modelo ng KomunikasyonMga Modelo ng Komunikasyon
Mga Modelo ng Komunikasyon
 
Real Numbers
Real NumbersReal Numbers
Real Numbers
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
 
Minto pyramid
Minto pyramidMinto pyramid
Minto pyramid
 
project on construction of house report.
project on construction of house report.project on construction of house report.
project on construction of house report.
 

Similar to Logic Formulation 3

Lecture 6.2 flow control repetition
Lecture 6.2  flow control repetitionLecture 6.2  flow control repetition
Lecture 6.2 flow control repetition
alvin567
 
05.system model and diagram
05.system model and diagram05.system model and diagram
05.system model and diagram
Rio Aurachman
 
Loop Concept and Array.ppt
Loop Concept and Array.pptLoop Concept and Array.ppt
Loop Concept and Array.ppt
AvinashJain66
 

Similar to Logic Formulation 3 (20)

PLF-Lesson-5 programming in TSU lec.pptx
PLF-Lesson-5 programming in TSU lec.pptxPLF-Lesson-5 programming in TSU lec.pptx
PLF-Lesson-5 programming in TSU lec.pptx
 
Lecture 6.2 flow control repetition
Lecture 6.2  flow control repetitionLecture 6.2  flow control repetition
Lecture 6.2 flow control repetition
 
2022-S1-IT2070-Lecture-06-Algorithms.pptx
2022-S1-IT2070-Lecture-06-Algorithms.pptx2022-S1-IT2070-Lecture-06-Algorithms.pptx
2022-S1-IT2070-Lecture-06-Algorithms.pptx
 
COMPUTER PROGRAMMING UNIT 1 Lecture 4
COMPUTER PROGRAMMING UNIT 1 Lecture 4COMPUTER PROGRAMMING UNIT 1 Lecture 4
COMPUTER PROGRAMMING UNIT 1 Lecture 4
 
CH-1.1 Introduction (1).pptx
CH-1.1 Introduction (1).pptxCH-1.1 Introduction (1).pptx
CH-1.1 Introduction (1).pptx
 
Algorithm and flowchart
Algorithm and flowchart Algorithm and flowchart
Algorithm and flowchart
 
05.system model and diagram
05.system model and diagram05.system model and diagram
05.system model and diagram
 
ch5.ppt
ch5.pptch5.ppt
ch5.ppt
 
Mit16 30 f10_lec01
Mit16 30 f10_lec01Mit16 30 f10_lec01
Mit16 30 f10_lec01
 
Object oriented programming18 control structures looping
Object oriented programming18 control structures loopingObject oriented programming18 control structures looping
Object oriented programming18 control structures looping
 
New c sharp3_features_(linq)_part_iii
New c sharp3_features_(linq)_part_iiiNew c sharp3_features_(linq)_part_iii
New c sharp3_features_(linq)_part_iii
 
Training Deep Networks with Backprop (D1L4 Insight@DCU Machine Learning Works...
Training Deep Networks with Backprop (D1L4 Insight@DCU Machine Learning Works...Training Deep Networks with Backprop (D1L4 Insight@DCU Machine Learning Works...
Training Deep Networks with Backprop (D1L4 Insight@DCU Machine Learning Works...
 
Simulink 1.pdf
Simulink 1.pdfSimulink 1.pdf
Simulink 1.pdf
 
introduction to modeling, Types of Models, Classification of mathematical mod...
introduction to modeling, Types of Models, Classification of mathematical mod...introduction to modeling, Types of Models, Classification of mathematical mod...
introduction to modeling, Types of Models, Classification of mathematical mod...
 
"A short and knowledgeable concept about Algorithm "
"A short and knowledgeable concept about Algorithm ""A short and knowledgeable concept about Algorithm "
"A short and knowledgeable concept about Algorithm "
 
CSI_06.pptx
CSI_06.pptxCSI_06.pptx
CSI_06.pptx
 
Simplex Algorithm
Simplex AlgorithmSimplex Algorithm
Simplex Algorithm
 
Types of Algorithms.ppt
Types of Algorithms.pptTypes of Algorithms.ppt
Types of Algorithms.ppt
 
Loop Concept and Array.ppt
Loop Concept and Array.pptLoop Concept and Array.ppt
Loop Concept and Array.ppt
 
cs1538.ppt
cs1538.pptcs1538.ppt
cs1538.ppt
 

More from deathful

Theories About Light
Theories About LightTheories About Light
Theories About Light
deathful
 
Microbiology Techniques
Microbiology TechniquesMicrobiology Techniques
Microbiology Techniques
deathful
 
Streak Plating
Streak PlatingStreak Plating
Streak Plating
deathful
 
Isolating Chromosomes
Isolating ChromosomesIsolating Chromosomes
Isolating Chromosomes
deathful
 

More from deathful (19)

Wastong Gamit ng mga Salita
Wastong Gamit ng mga SalitaWastong Gamit ng mga Salita
Wastong Gamit ng mga Salita
 
UST ICS Freshmen Orientation
UST ICS Freshmen OrientationUST ICS Freshmen Orientation
UST ICS Freshmen Orientation
 
College Algebra
College AlgebraCollege Algebra
College Algebra
 
Special Products
Special ProductsSpecial Products
Special Products
 
Theories About Light
Theories About LightTheories About Light
Theories About Light
 
Provision For Depreciation
Provision For DepreciationProvision For Depreciation
Provision For Depreciation
 
Acid-Base Titration & Calculations
Acid-Base Titration & CalculationsAcid-Base Titration & Calculations
Acid-Base Titration & Calculations
 
Microbiology Techniques
Microbiology TechniquesMicrobiology Techniques
Microbiology Techniques
 
Streak Plating
Streak PlatingStreak Plating
Streak Plating
 
Egg Windowing
Egg WindowingEgg Windowing
Egg Windowing
 
Egg Injection
Egg InjectionEgg Injection
Egg Injection
 
Colony Counter Compatibility Mode
Colony Counter Compatibility ModeColony Counter Compatibility Mode
Colony Counter Compatibility Mode
 
Isolating Chromosomes
Isolating ChromosomesIsolating Chromosomes
Isolating Chromosomes
 
Autoclave
AutoclaveAutoclave
Autoclave
 
Aseptic Technique
Aseptic TechniqueAseptic Technique
Aseptic Technique
 
Agar Culture Medium
Agar Culture MediumAgar Culture Medium
Agar Culture Medium
 
Allium Cepa Genotoxicity Test
Allium Cepa Genotoxicity TestAllium Cepa Genotoxicity Test
Allium Cepa Genotoxicity Test
 
Tilapia Nilotica As A Biological Indicator
Tilapia Nilotica As A Biological IndicatorTilapia Nilotica As A Biological Indicator
Tilapia Nilotica As A Biological Indicator
 
Pagkonsumo
PagkonsumoPagkonsumo
Pagkonsumo
 

Recently uploaded

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
mphochane1998
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
jaanualu31
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
Kamal Acharya
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
AldoGarca30
 

Recently uploaded (20)

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to Computers
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
Wadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptxWadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptx
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech Civil
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 

Logic Formulation 3

  • 1. A Beginner’s Guide to Programming Logic, Introductory Chapter 3 Understanding Structure
  • 2. Objectives In this chapter, you will learn about: • The features of unstructured spaghetti code • The three basic structures—sequence, selection, and loop • Using a priming input to structure a program • The need for structure • Recognizing structure • Structuring and modularizing unstructured logic A Beginner's Guide to Programming Logic, Introductory 2
  • 3. Understanding Unstructured Spaghetti Code • Spaghetti code – Logically snarled program statements – Can be the result of poor program design – Programs often work but are difficult to read and maintain – Convoluted logic usually requires more code • Unstructured programs – Do not follow the rules of structured logic • Structured programs – Do follow rules of structured logic A Beginner's Guide to Programming Logic, Introductory 3
  • 4. Figure 3-1 Spaghetti code logic for washing a dog A Beginner's Guide to Programming Logic, Introductory 4
  • 5. Understanding the Three Basic Structures • Structure – Basic unit of programming logic – Sequence • Perform actions in order • No branching or skipping any task – Selection (decision) • Ask a question, take one of two actions • Dual-alternative or single-alternative ifs – Loop • Repeat actions based on answer to a question A Beginner's Guide to Programming Logic, Introductory 5
  • 6. Understanding the Three Basic Structures (continued) Figure 3-2 Sequence structure A Beginner's Guide to Programming Logic, Introductory 6
  • 7. Understanding the Three Basic Structures (continued) Figure 3-3 Selection structure A Beginner's Guide to Programming Logic, Introductory 7
  • 8. Understanding the Three Basic Structures (continued) • Dual-alternative if – Contains two alternatives – If-then-else structure if someCondition is true then do oneProcess else do theOtherProcess A Beginner's Guide to Programming Logic, Introductory 8
  • 9. Understanding the Three Basic Structures (continued) • Single-alternative if if employee belongs to dentalPlan then deduct $40 from employeeGrossPay – Else clause is not required • null case – Situation where nothing is done A Beginner's Guide to Programming Logic, Introductory 9
  • 10. Understanding the Three Basic Structures (continued) Figure 3-4 Single-alternative selection structure A Beginner's Guide to Programming Logic, Introductory 10
  • 11. Understanding the Three Basic Structures (continued) • Loop structure – Repeats a set of actions based on the answer to a question • Loop body – Also called repetition or iteration – Question is asked first in the most common form of loop – while … do or while loop A Beginner's Guide to Programming Logic, Introductory 11
  • 12. Understanding the Three Basic Structures (continued) Figure 3-5 Loop structure A Beginner's Guide to Programming Logic, Introductory 12
  • 13. Understanding the Three Basic Structures (continued) • Loop structure while testCondition continues to be true do someProcess while quantityInInventory remains low continue to orderItems A Beginner's Guide to Programming Logic, Introductory 13
  • 14. Understanding the Three Basic Structures (continued) • All logic problems can be solved using only these three structures • Structures can be combined in an infinite number of ways • Stacking – Attaching structures end-to-end • End-structure statements – Indicate the end of a structure – The endif statement ends an if-then-else structure – The endwhile ends a loop structure A Beginner's Guide to Programming Logic, Introductory 14
  • 15. Understanding the Three Basic Structures (continued) Figure 3-6 Structured flowchart and pseudocode with three stacked structures A Beginner's Guide to Programming Logic, Introductory 15
  • 16. Understanding the Three Basic Structures (continued) • Any individual task or step in a structure can be replaced by a structure • Nesting – Placing one structure within another – Indent the nested structure’s statements • Block – Group of statements that execute as a single unit A Beginner's Guide to Programming Logic, Introductory 16
  • 17. Understanding the Three Basic Structures (continued) Figure 3-7 Flowchart and pseudocode showing nested structures—a sequence nested within a selection A Beginner's Guide to Programming Logic, Introductory 17
  • 18. Understanding the Three Basic Structures (continued) Figure 3-8 Flowchart and pseudocode showing nested structures—a loop nested within a sequence, nested within a selection A Beginner's Guide to Programming Logic, Introductory 18
  • 19. Understanding the Three Basic Structures (continued) Figure 3-9 Flowchart and pseudocode for loop within selection within sequence within selection A Beginner's Guide to Programming Logic, Introductory 19
  • 20. Understanding the Three Basic Structures (continued) • Structured programs have the following characteristics: – Include only combinations of the three basic structures – Each of the structures has a single entry point and a single exit point – Structures can be stacked or connected to one another only at their entry or exit points – Any structure can be nested within another structure A Beginner's Guide to Programming Logic, Introductory 20
  • 21. Using a Priming Input to Structure a Program • Priming read (or priming input) – Reads the first input data record – Outside the loop that reads the rest of the records – Helps keep the program structured • Analyze a flowchart for structure one step at a time • Watch for unstructured loops that do not follow this order – First ask a question – Take action based on the answer – Return to ask the question again A Beginner's Guide to Programming Logic, Introductory 21
  • 22. Using a Priming Input to Structure a Program (continued) Figure 3-15 Structured, but nonfunctional, flowchart of number-doubling problem A Beginner's Guide to Programming Logic, Introductory 22
  • 23. Using a Priming Input to Structure a Program (continued) Figure 3-16 Functional but unstructured flowchart A Beginner's Guide to Programming Logic, Introductory 23
  • 24. Using a Priming Input to Structure a Program (continued) • Priming read sets up the process so the loop can be structured • To analyze a flowchart’s structure, try writing pseudocode for it start get inputNumber while not eof calculatedAnswer = inputNumber * 2 print calculatedAnswer get inputNumber endwhile stop A Beginner's Guide to Programming Logic, Introductory 24
  • 25. Figure 3-17 Functional, structured flowchart and pseudocode for the number-doubling problem A Beginner's Guide to Programming Logic, Introductory 25
  • 26. Figure 3-18 Structured but incorrect solution to the number-doubling problem A Beginner's Guide to Programming Logic, Introductory 26
  • 27. Understanding the Reasons for Structure • Clarity • Professionalism • Efficiency • Ease of maintenance • Supports modularity A Beginner's Guide to Programming Logic, Introductory 27
  • 28. Recognizing Structure • Any set of instructions can be expressed in structured format • Any task to which you can apply rules can be expressed logically using sequence, selection, loop • It can be difficult to detect whether a flowchart is structured A Beginner's Guide to Programming Logic, Introductory 28
  • 29. Recognizing Structure (continued) Figure 3-20 Example 2 A Beginner's Guide to Programming Logic, Introductory 29
  • 30. Recognizing Structure (continued) Figure 3-21 Example 3 A Beginner's Guide to Programming Logic, Introductory 30
  • 31. Recognizing Structure (continued) • Single process like G is part of an acceptable structure – At least the beginning of a sequence structure Figure 3-22 Untangling Example 3, first step A Beginner's Guide to Programming Logic, Introductory 31
  • 32. Recognizing Structure (continued) • H begins a selection structure – Sequences never have decisions in them – Logic never returns to G Figure 3-23 Untangling Example 3, second step A Beginner's Guide to Programming Logic, Introductory 32
  • 33. Recognizing Structure (continued) • Pull up on the flowline from the left side of H Figure 3-24 Untangling Example 3, third step A Beginner's Guide to Programming Logic, Introductory 33
  • 34. Recognizing Structure (continued) • Next, pull up the flowline on the right side of H Figure 3-25 Untangling Example 3, fourth step A Beginner's Guide to Programming Logic, Introductory 34
  • 35. Recognizing Structure (continued) • Pull up the flowline on the left side of I and untangle it from the B selection by repeating J Figure 3-26 Untangling Example 3, fifth step A Beginner's Guide to Programming Logic, Introductory 35
  • 36. Recognizing Structure (continued) • Now pull up the flowline on the right side of I Figure 3-27 Untangling Example 3, sixth step A Beginner's Guide to Programming Logic, Introductory 36
  • 37. Recognizing Structure (continued) • Bring together the loose ends of I and of H Figure 3-28 Finished flowchart and pseudocode for untangling Example 3 A Beginner's Guide to Programming Logic, Introductory 37
  • 38. Structuring and Modularizing Unstructured Logic • Dog-washing process – Unstructured – Can be reconfigured to be structured • First step simple sequence Figure 3-29 Washing the dog, part 1 A Beginner's Guide to Programming Logic, Introductory 38
  • 39. Structuring and Modularizing Unstructured Logic (continued) • After the dog runs away – Catch the dog and determine whether he runs away again – A loop begins Figure 3-30 Washing the dog, part 2 A Beginner's Guide to Programming Logic, Introductory 39
  • 40. Figure 3-31 Washing the dog, part 3 A Beginner's Guide to Programming Logic, Introductory 40
  • 41. Figure 3-33 Structured dog-washing flowchart and pseudocode A Beginner's Guide to Programming Logic, Introductory 41
  • 42. Figure 3-34 Modularized version of the dog-washing program A Beginner's Guide to Programming Logic, Introductory 42
  • 43. Summary • Spaghetti code – Snarled program logic • Three basic structures – Sequence, selection, and loop – Combined by stacking and nesting • Priming read – Statement that reads the first input data record A Beginner's Guide to Programming Logic, Introductory 43
  • 44. Summary (continued) • Structured techniques promote: – Clarity – Professionalism – Efficiency – Modularity • Flowchart can be made structured by untangling A Beginner's Guide to Programming Logic, Introductory 44