SlideShare a Scribd company logo
1 of 25
Understanding Structure
Understanding the Three Basic Structures
In the mid-1960s, mathematicians proved that any program, no matter how
complicated, can be constructed using one or more of only three structures.
A structure is a basic unit of programming logic; each structure is one of the
following:
• Sequence
• Selection
• Loop
With these three structures alone, you can diagram any task, from doubling
a number to performing brain surgery. You can diagram each structure with
a specific configuration of flowchart symbols.
Understanding the Three Basic Structures
The first of these three basic structures is a sequence, as
shown in the figure. With a sequence structure, you
perform an action or task, and then you perform the next
action, in order. A sequence can contain any number of
tasks, but there is no option to branch off and skip any of
the tasks. Once you start a series of actions in a sequence,
you must continue step by step until the sequence ends.
Understanding the Three Basic Structures
As an example, driving directions often are listed as a sequence. To tell a
friend how to get to your house from school, you might provide the
following sequence, in which one step follows the other and no steps can be
skipped:
go north on First Avenue for 3 miles
turn left on Washington Boulevard
go west on Washington for 2 miles
stop at 634 Washington
Understanding the Three Basic Structures
The second of the three structures is a
selection structure or decision
structure, as shown in the figure. With
this structure, you ask a question and,
depending on the answer, you take one of
two courses of action. Then, no matter
which path you follow, you continue with
the next task.
Understanding the Three Basic Structures
Some people call the selection structure an if-then-else because it fits the
following statement:
if someCondition is true then
do oneProcess
else
do theOtherProcess
endif
Understanding the Three Basic Structures
For example, you might provide part of the directions to your house as
follows:
if traffic is backed up on Washington Boulevard then
continue for 1 block on First Avenue and turn left on Adams Lane
else
turn left on Washington Boulevard
endif
Understanding the Three Basic Structures
Similarly, a payroll program might include a statement such as:
if hoursWorked is more than 40 then
calculate regularPay and overtimePay
else
calculate regularPay
endif
Understanding the Three Basic Structures
These if-else examples can also be
called dual-alternative ifs (or
dual-alternative selections),
because they contain two
alternatives—the action taken when
the tested condition is true and the
action taken when it is false. Note
that it is perfectly correct for one
branch of the selection to be a “do
nothing” branch. In each of the
following examples, an action is
taken only when the tested
condition is true:
if it is raining then
take an umbrella
endif
if employee participates in the dental plan then
deduct $40 from employee gross pay
endif
Understanding the Three Basic Structures
The previous examples without else clauses
are single-alternative ifs (or single-
alternative selections); a diagram of their
structure is shown in the figure. In these
cases, you do not take any special action if
it is not raining or if the employee does not
belong to the dental plan. The case in
which nothing is done is often called the
null case.
Understanding the Three Basic Structures
The last of the three basic structures, shown in Figure 3-5, is a loop. In a
loop structure, you continue to repeat actions while a condition remains
true. The action or actions that occur
within the loop are the loop body. In
the most common type of loop, a
condition is evaluated; if the answer is
true, you execute the loop body and
evaluate the condition again. If the
condition is still true, you execute the
loop body again and then reevaluate the
original condition. This continues until
the condition becomes false, and then you exit the structure.
Understanding the Three Basic Structures
You may hear programmers refer to looping as repetition or iteration.
Some programmers call this structure a while…do, or more simply, a while
loop, because it fits the following statement:
while testCondition continues to be true
do someProcess
endwhile
Understanding the Three Basic Structures
When you provide directions to your house, part of the directions might be:
while the address of the house you are passing remains below 634
travel forward to the next house
look at the address on the house
endwhile
Understanding the Three Basic Structures
You encounter examples of looping every day, as in each of the following:
while you continue to be hungry
take another bite of food
determine whether you still feel hungry
endwhile
while unread pages remain in the reading assignment
read another unread page
determine whether there are more pages to be read
endwhile
Understanding the Three Basic Structures
All logic problems can be solved using only these three structures—
sequence, selection, and loop. The structures can be combined in an infinite
number of ways. For example, you can have a sequence of tasks followed by
a selection, or a loop followed by a sequence. Attaching structures end to
end is called stacking structures.
Understanding the Three Basic Structures
For example, the figure shows a
structured flowchart achieved by
stacking structures, and shows
pseudocode that follows the
flowchart logic.
The pseudocode shows a sequence,
followed by a selection, followed by
a loop.
Understanding the Three Basic Structures
First stepA and stepB execute in sequence. Then a selection structure starts
with the test of conditionC. The instruction that follows the if clause (stepD)
occurs when its tested condition is true, the instruction that follows else
(stepE) occurs when the tested condition is false, and any instructions that
follow endif occur in either case. In other words, statements beyond the
endif statement are “outside” the decision structure. Similarly, the
endwhile statement shows where the loop structure ends. While conditionF
continues to be true, stepG continues to execute. If any statements
followed the endwhile statement, they would be outside of, and not a part
of, the loop.
Understanding the Three Basic Structures
Besides stacking structures, you can
replace any individual steps in a
structured flowchart diagram or
pseudocode with additional structures.
In other words, any sequence,
selection, or loop can contain other
sequences, selections, or loops. For
example, you can have a sequence of
three tasks on one side of a selection,
as shown. Placing a structure within
another structure is called nesting
structures.
Understanding the Three Basic Structures
In the pseudocode for the logic
shown, the indentation shows
that all three statements (stepJ,
stepK, and stepL) must execute
if conditionH is true. The three
statements constitute a block,
or a group of statements that
executes as a single unit.
Understanding the Three Basic Structures
In place of one of the steps in
the sequence, you can insert
another structure. In the figure,
the process named stepK has
been replaced with a loop
structure that begins with a test
of the condition named
conditionM.
Understanding the Three Basic Structures
In the pseudocode shown, notice that if and endif are vertically aligned.
This shows that they are all “on the same level.” Similarly, stepJ, while,
endwhile, and stepL are aligned, and they are evenly indented. In the
flowchart, you could draw a vertical line through the symbols containing
stepJ, the entry and exit points of the while loop, and stepL. The flowchart
and the pseudocode represent exactly the same logic.
Understanding the Three Basic Structures
There is no limit to the number of levels you
can create when you nest and stack
structures. For example, the figure shows
logic that has been made more complicated
by replacing stepN with a selection. The
structure that performs stepP or stepQ
based on the outcome of conditionO is
nested within the loop that is controlled by
conditionM. In the pseudocode, notice how
the if, else, and endif that describe the
condition selection are aligned with each
other and within the while structure that is
controlled by conditionM.
Understanding the Three Basic Structures
The possible combinations of logical structures are endless, but each
segment of a structured
program is a sequence, a
selection, or a loop. The
three structures are shown
together. Notice that each
structure has one entry
point and one exit point.
One structure can attach to
another only at one of these
points.
Summary
A structured program has the following characteristics:
• A structured program includes only combinations of the three basic
structures— sequence, selection, and loop. Any structured program might
contain one, two, or all three types of 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.
THANK YOU

More Related Content

What's hot

What's hot (20)

Consistency protocols
Consistency protocolsConsistency protocols
Consistency protocols
 
Database design & Normalization (1NF, 2NF, 3NF)
Database design & Normalization (1NF, 2NF, 3NF)Database design & Normalization (1NF, 2NF, 3NF)
Database design & Normalization (1NF, 2NF, 3NF)
 
Android Fragment
Android FragmentAndroid Fragment
Android Fragment
 
Kernel I/O subsystem
Kernel I/O subsystemKernel I/O subsystem
Kernel I/O subsystem
 
Dining philosopher problem operating system
Dining philosopher problem operating system Dining philosopher problem operating system
Dining philosopher problem operating system
 
Control structure
Control structureControl structure
Control structure
 
Pge Replacement Algorithm.pdf
Pge Replacement Algorithm.pdfPge Replacement Algorithm.pdf
Pge Replacement Algorithm.pdf
 
Linked list
Linked listLinked list
Linked list
 
Normalization in DBMS
Normalization in DBMSNormalization in DBMS
Normalization in DBMS
 
Linked list
Linked listLinked list
Linked list
 
Introduction to fragments in android
Introduction to fragments in androidIntroduction to fragments in android
Introduction to fragments in android
 
Types of loops in c language
Types of loops in c languageTypes of loops in c language
Types of loops in c language
 
Operating System - Types Of Operating System Unit-1
Operating System - Types Of Operating System Unit-1Operating System - Types Of Operating System Unit-1
Operating System - Types Of Operating System Unit-1
 
Concurrent transactions
Concurrent transactionsConcurrent transactions
Concurrent transactions
 
What is Switch Case?
What is Switch Case?What is Switch Case?
What is Switch Case?
 
Normalisation revision
Normalisation revisionNormalisation revision
Normalisation revision
 
Loops in c
Loops in cLoops in c
Loops in c
 
Codd's rules
Codd's rulesCodd's rules
Codd's rules
 
15. Transactions in DBMS
15. Transactions in DBMS15. Transactions in DBMS
15. Transactions in DBMS
 
User Interface Design- Module 3 Menus
User Interface Design- Module 3 MenusUser Interface Design- Module 3 Menus
User Interface Design- Module 3 Menus
 

Similar to Understanding the Three Basic Structures

C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, LoopingMURALIDHAR R
 
ntroduction to Ethology Part 2 Quantifying BehaviorBio 112 Lab, S.docx
ntroduction to Ethology Part 2 Quantifying BehaviorBio 112 Lab, S.docxntroduction to Ethology Part 2 Quantifying BehaviorBio 112 Lab, S.docx
ntroduction to Ethology Part 2 Quantifying BehaviorBio 112 Lab, S.docxgabriellabre8fr
 
Introduction to Ethology Part 2 Quantifying BehaviorBio 112 Lab.docx
Introduction to Ethology Part 2 Quantifying BehaviorBio 112 Lab.docxIntroduction to Ethology Part 2 Quantifying BehaviorBio 112 Lab.docx
Introduction to Ethology Part 2 Quantifying BehaviorBio 112 Lab.docxmariuse18nolet
 
Lesson flow charts 1
Lesson flow charts 1Lesson flow charts 1
Lesson flow charts 1Lexume1
 
In order to study and measure behavior you must break the behavi.docx
In order to study and measure behavior you must break the behavi.docxIn order to study and measure behavior you must break the behavi.docx
In order to study and measure behavior you must break the behavi.docxrochellscroop
 
Intro To C++ - Class 09 - Control Statements: Part 1
Intro To C++ - Class 09 - Control Statements: Part 1Intro To C++ - Class 09 - Control Statements: Part 1
Intro To C++ - Class 09 - Control Statements: Part 1Blue Elephant Consulting
 
Introduction to loops cpu
Introduction to loops  cpuIntroduction to loops  cpu
Introduction to loops cpuHarsh Gupta
 
Conditional statements & Loops
Conditional statements & LoopsConditional statements & Loops
Conditional statements & Loopssaifullahbhatti99
 
Mathematical Logic Part 2
Mathematical Logic Part 2Mathematical Logic Part 2
Mathematical Logic Part 2blaircomp2003
 
EU Assistant Exams Workbook - Volume I
EU Assistant Exams Workbook - Volume IEU Assistant Exams Workbook - Volume I
EU Assistant Exams Workbook - Volume Ieuinmotion
 
Eu assistant exams_workbook_eu_training
Eu assistant exams_workbook_eu_trainingEu assistant exams_workbook_eu_training
Eu assistant exams_workbook_eu_trainingeuinmotion
 
Wk 5 Individual Preparing for Working in Teams [due Day#]Top of.docx
Wk 5 Individual Preparing for Working in Teams [due Day#]Top of.docxWk 5 Individual Preparing for Working in Teams [due Day#]Top of.docx
Wk 5 Individual Preparing for Working in Teams [due Day#]Top of.docxhelzerpatrina
 

Similar to Understanding the Three Basic Structures (20)

C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, Looping
 
ntroduction to Ethology Part 2 Quantifying BehaviorBio 112 Lab, S.docx
ntroduction to Ethology Part 2 Quantifying BehaviorBio 112 Lab, S.docxntroduction to Ethology Part 2 Quantifying BehaviorBio 112 Lab, S.docx
ntroduction to Ethology Part 2 Quantifying BehaviorBio 112 Lab, S.docx
 
Introduction to Ethology Part 2 Quantifying BehaviorBio 112 Lab.docx
Introduction to Ethology Part 2 Quantifying BehaviorBio 112 Lab.docxIntroduction to Ethology Part 2 Quantifying BehaviorBio 112 Lab.docx
Introduction to Ethology Part 2 Quantifying BehaviorBio 112 Lab.docx
 
Lesson flow charts 1
Lesson flow charts 1Lesson flow charts 1
Lesson flow charts 1
 
In order to study and measure behavior you must break the behavi.docx
In order to study and measure behavior you must break the behavi.docxIn order to study and measure behavior you must break the behavi.docx
In order to study and measure behavior you must break the behavi.docx
 
Intro To C++ - Class 09 - Control Statements: Part 1
Intro To C++ - Class 09 - Control Statements: Part 1Intro To C++ - Class 09 - Control Statements: Part 1
Intro To C++ - Class 09 - Control Statements: Part 1
 
Time series
Time seriesTime series
Time series
 
Chapter 5 (final)
Chapter 5 (final)Chapter 5 (final)
Chapter 5 (final)
 
Introduction to loops cpu
Introduction to loops  cpuIntroduction to loops  cpu
Introduction to loops cpu
 
Loops
LoopsLoops
Loops
 
Conditional statements & Loops
Conditional statements & LoopsConditional statements & Loops
Conditional statements & Loops
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 
Mathematical Logic Part 2
Mathematical Logic Part 2Mathematical Logic Part 2
Mathematical Logic Part 2
 
Logic Summaries
Logic SummariesLogic Summaries
Logic Summaries
 
Java chapter 3
Java chapter 3Java chapter 3
Java chapter 3
 
EU Assistant Exams Workbook - Volume I
EU Assistant Exams Workbook - Volume IEU Assistant Exams Workbook - Volume I
EU Assistant Exams Workbook - Volume I
 
Eu assistant exams_workbook_eu_training
Eu assistant exams_workbook_eu_trainingEu assistant exams_workbook_eu_training
Eu assistant exams_workbook_eu_training
 
Wk 5 Individual Preparing for Working in Teams [due Day#]Top of.docx
Wk 5 Individual Preparing for Working in Teams [due Day#]Top of.docxWk 5 Individual Preparing for Working in Teams [due Day#]Top of.docx
Wk 5 Individual Preparing for Working in Teams [due Day#]Top of.docx
 
C Constructs (C Statements & Loop)
C Constructs (C Statements & Loop)C Constructs (C Statements & Loop)
C Constructs (C Statements & Loop)
 
02 - Prepcode
02 - Prepcode02 - Prepcode
02 - Prepcode
 

More from Ar Kyu Dee

Learning outcome 2 prepare make pcb modules
Learning outcome 2 prepare make pcb modulesLearning outcome 2 prepare make pcb modules
Learning outcome 2 prepare make pcb modulesAr Kyu Dee
 
Learning outcome 1 prepare to assemble electronic products
Learning outcome 1 prepare to assemble electronic productsLearning outcome 1 prepare to assemble electronic products
Learning outcome 1 prepare to assemble electronic productsAr Kyu Dee
 
Using Pseudocode Statements and Flowchart Symbols
Using Pseudocode Statements and Flowchart SymbolsUsing Pseudocode Statements and Flowchart Symbols
Using Pseudocode Statements and Flowchart SymbolsAr Kyu Dee
 
Understanding Simple Program Logic
Understanding Simple Program LogicUnderstanding Simple Program Logic
Understanding Simple Program LogicAr Kyu Dee
 
Understanding Computer Systems
Understanding Computer SystemsUnderstanding Computer Systems
Understanding Computer SystemsAr Kyu Dee
 

More from Ar Kyu Dee (7)

Cube
CubeCube
Cube
 
Cuboid
CuboidCuboid
Cuboid
 
Learning outcome 2 prepare make pcb modules
Learning outcome 2 prepare make pcb modulesLearning outcome 2 prepare make pcb modules
Learning outcome 2 prepare make pcb modules
 
Learning outcome 1 prepare to assemble electronic products
Learning outcome 1 prepare to assemble electronic productsLearning outcome 1 prepare to assemble electronic products
Learning outcome 1 prepare to assemble electronic products
 
Using Pseudocode Statements and Flowchart Symbols
Using Pseudocode Statements and Flowchart SymbolsUsing Pseudocode Statements and Flowchart Symbols
Using Pseudocode Statements and Flowchart Symbols
 
Understanding Simple Program Logic
Understanding Simple Program LogicUnderstanding Simple Program Logic
Understanding Simple Program Logic
 
Understanding Computer Systems
Understanding Computer SystemsUnderstanding Computer Systems
Understanding Computer Systems
 

Recently uploaded

21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptxJoelynRubio1
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfstareducators107
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationNeilDeclaro1
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111GangaMaiya1
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSAnaAcapella
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 

Recently uploaded (20)

21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health Education
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 

Understanding the Three Basic Structures

  • 2. Understanding the Three Basic Structures In the mid-1960s, mathematicians proved that any program, no matter how complicated, can be constructed using one or more of only three structures. A structure is a basic unit of programming logic; each structure is one of the following: • Sequence • Selection • Loop With these three structures alone, you can diagram any task, from doubling a number to performing brain surgery. You can diagram each structure with a specific configuration of flowchart symbols.
  • 3. Understanding the Three Basic Structures The first of these three basic structures is a sequence, as shown in the figure. With a sequence structure, you perform an action or task, and then you perform the next action, in order. A sequence can contain any number of tasks, but there is no option to branch off and skip any of the tasks. Once you start a series of actions in a sequence, you must continue step by step until the sequence ends.
  • 4. Understanding the Three Basic Structures As an example, driving directions often are listed as a sequence. To tell a friend how to get to your house from school, you might provide the following sequence, in which one step follows the other and no steps can be skipped: go north on First Avenue for 3 miles turn left on Washington Boulevard go west on Washington for 2 miles stop at 634 Washington
  • 5. Understanding the Three Basic Structures The second of the three structures is a selection structure or decision structure, as shown in the figure. With this structure, you ask a question and, depending on the answer, you take one of two courses of action. Then, no matter which path you follow, you continue with the next task.
  • 6. Understanding the Three Basic Structures Some people call the selection structure an if-then-else because it fits the following statement: if someCondition is true then do oneProcess else do theOtherProcess endif
  • 7. Understanding the Three Basic Structures For example, you might provide part of the directions to your house as follows: if traffic is backed up on Washington Boulevard then continue for 1 block on First Avenue and turn left on Adams Lane else turn left on Washington Boulevard endif
  • 8. Understanding the Three Basic Structures Similarly, a payroll program might include a statement such as: if hoursWorked is more than 40 then calculate regularPay and overtimePay else calculate regularPay endif
  • 9. Understanding the Three Basic Structures These if-else examples can also be called dual-alternative ifs (or dual-alternative selections), because they contain two alternatives—the action taken when the tested condition is true and the action taken when it is false. Note that it is perfectly correct for one branch of the selection to be a “do nothing” branch. In each of the following examples, an action is taken only when the tested condition is true: if it is raining then take an umbrella endif if employee participates in the dental plan then deduct $40 from employee gross pay endif
  • 10. Understanding the Three Basic Structures The previous examples without else clauses are single-alternative ifs (or single- alternative selections); a diagram of their structure is shown in the figure. In these cases, you do not take any special action if it is not raining or if the employee does not belong to the dental plan. The case in which nothing is done is often called the null case.
  • 11. Understanding the Three Basic Structures The last of the three basic structures, shown in Figure 3-5, is a loop. In a loop structure, you continue to repeat actions while a condition remains true. The action or actions that occur within the loop are the loop body. In the most common type of loop, a condition is evaluated; if the answer is true, you execute the loop body and evaluate the condition again. If the condition is still true, you execute the loop body again and then reevaluate the original condition. This continues until the condition becomes false, and then you exit the structure.
  • 12. Understanding the Three Basic Structures You may hear programmers refer to looping as repetition or iteration. Some programmers call this structure a while…do, or more simply, a while loop, because it fits the following statement: while testCondition continues to be true do someProcess endwhile
  • 13. Understanding the Three Basic Structures When you provide directions to your house, part of the directions might be: while the address of the house you are passing remains below 634 travel forward to the next house look at the address on the house endwhile
  • 14. Understanding the Three Basic Structures You encounter examples of looping every day, as in each of the following: while you continue to be hungry take another bite of food determine whether you still feel hungry endwhile while unread pages remain in the reading assignment read another unread page determine whether there are more pages to be read endwhile
  • 15. Understanding the Three Basic Structures All logic problems can be solved using only these three structures— sequence, selection, and loop. The structures can be combined in an infinite number of ways. For example, you can have a sequence of tasks followed by a selection, or a loop followed by a sequence. Attaching structures end to end is called stacking structures.
  • 16. Understanding the Three Basic Structures For example, the figure shows a structured flowchart achieved by stacking structures, and shows pseudocode that follows the flowchart logic. The pseudocode shows a sequence, followed by a selection, followed by a loop.
  • 17. Understanding the Three Basic Structures First stepA and stepB execute in sequence. Then a selection structure starts with the test of conditionC. The instruction that follows the if clause (stepD) occurs when its tested condition is true, the instruction that follows else (stepE) occurs when the tested condition is false, and any instructions that follow endif occur in either case. In other words, statements beyond the endif statement are “outside” the decision structure. Similarly, the endwhile statement shows where the loop structure ends. While conditionF continues to be true, stepG continues to execute. If any statements followed the endwhile statement, they would be outside of, and not a part of, the loop.
  • 18. Understanding the Three Basic Structures Besides stacking structures, you can replace any individual steps in a structured flowchart diagram or pseudocode with additional structures. In other words, any sequence, selection, or loop can contain other sequences, selections, or loops. For example, you can have a sequence of three tasks on one side of a selection, as shown. Placing a structure within another structure is called nesting structures.
  • 19. Understanding the Three Basic Structures In the pseudocode for the logic shown, the indentation shows that all three statements (stepJ, stepK, and stepL) must execute if conditionH is true. The three statements constitute a block, or a group of statements that executes as a single unit.
  • 20. Understanding the Three Basic Structures In place of one of the steps in the sequence, you can insert another structure. In the figure, the process named stepK has been replaced with a loop structure that begins with a test of the condition named conditionM.
  • 21. Understanding the Three Basic Structures In the pseudocode shown, notice that if and endif are vertically aligned. This shows that they are all “on the same level.” Similarly, stepJ, while, endwhile, and stepL are aligned, and they are evenly indented. In the flowchart, you could draw a vertical line through the symbols containing stepJ, the entry and exit points of the while loop, and stepL. The flowchart and the pseudocode represent exactly the same logic.
  • 22. Understanding the Three Basic Structures There is no limit to the number of levels you can create when you nest and stack structures. For example, the figure shows logic that has been made more complicated by replacing stepN with a selection. The structure that performs stepP or stepQ based on the outcome of conditionO is nested within the loop that is controlled by conditionM. In the pseudocode, notice how the if, else, and endif that describe the condition selection are aligned with each other and within the while structure that is controlled by conditionM.
  • 23. Understanding the Three Basic Structures The possible combinations of logical structures are endless, but each segment of a structured program is a sequence, a selection, or a loop. The three structures are shown together. Notice that each structure has one entry point and one exit point. One structure can attach to another only at one of these points.
  • 24. Summary A structured program has the following characteristics: • A structured program includes only combinations of the three basic structures— sequence, selection, and loop. Any structured program might contain one, two, or all three types of 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.