0% found this document useful (0 votes)
9 views

Unit 3 - Structure

Uploaded by

Augustin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Unit 3 - Structure

Uploaded by

Augustin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Structure

Learning Outcomes
• The disadvantages of unstructured spaghetti
code
• The three basic structures—sequence, selection,
and loop
• The need for structure
• Recognizing structure
The Disadvantages of Unstructured
Spaghetti Code
When you add several thousand instructions to a program,
including several hundred decisions, it is easy to create a
complicated mess. The popular name for logically snarled
program statements is spaghetti code, because the logic is as
hard to follow.
Not only is spaghetti code confusing, the programs that contain
it are prone to error, difficult to reuse, and hard to use as
building blocks for larger applications. Programs that use
spaghetti code logic are unstructured programs; that is, they do
not follow the rules of structured logic.
Structured programs do follow those rules, and eliminate the
problems caused by spaghetti code.
Image of Spaghetti Code
Three Basic Structures
• A structure is a basic unit of programming logic;
each structure is one of the following:
– sequence
– selection
– loop
Sequence structure
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.
Sequence structure
Example - Sequence structure
Selection structure or decision
structure
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. (In other words, a flowchart that
describes a selection structure must begin with a
decision symbol, and the branches of the decision must
join at the bottom of the structure. Pseudocode that
describes a selection structure must start with if.
Pseudocode uses the end-structure statement end if to
clearly show where the structure ends.)
Selection structure
Syntax - Selection structure
Example - Selection structure
Single-alternative selection structure
Example1: Draw a structured flowchart for given
Pseudocode
Start
Input Number1
Input Number2
If Number1>Number2 then
larger_number = Number1
else:
larger_number=Number2
Endif
Output larger_number
Stop
NB: Predict the output if Number1 = 10 & Number2 20
Loop structure
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.
Cont..
(In other words, a flowchart that describes a loop
structure always begins with a decision symbol
that has a branch that returns to a spot prior to
the decision. Pseudocode that describes a loop
starts with while and ends with the end-structure
statement end while.) You may hear programmers
refer to looping as repetition or iteration.
Loop structure
While loop
Example2: While loop

Start
While True
Output statement “ I am stuck inside this loop”
Endwhile

NB: Predict the output of above loop


Stacking 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.
Structured flowchart and
pseudocode with three stacked
structures
Nesting structures
Placing a structure within another structure is
called nesting structures.
Example3:Nested Loops

Start
largest_number = -999999999
Input first number OR -1 to Stop
While number != -1
If number > largest_number then
largest_number = number
Input next number OR -1 to stop
EndWhile
Output largest_number
Stop
NB: Predict output if test data for number is 4, 3,20,10
Note: Each structure has one entry point and one exit point. One structure can attach
to another only at one of these points.
Summary of a structured program
• 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.
True/False
1. Each structure in structured programming is a
sequence, selection, or loop.
2. All logic problems can be solved using only
three structures—sequence, selection, and
loop.
3. The three structures cannot be combined in a
single program.
Difference between selection and
loop structures
• In a selection structure, the logic goes in one of two
directions after the question, and then the flow
comes back together; the question is not asked a
second time within the structure.
• In a loop, if the answer to the question results in the
loop being entered and the loop statements
executing, then the logic returns to the question that
started the loop. When the body of a loop executes,
the question that controls the loop is always asked
again.
Reasons for Structure
• Clarity
• Professionalism
• Efficiency
• Maintenance
• Modularity
True/False
1. Structured programs are clearer than
unstructured programs.
2. You and other programmers will find it easier
to modify and maintain structured programs
as changes are required in the future.
3. Structured programs are not easily divided
into parts, making them less prone to error.
References
Required reading:
• Programming Logic and Design, Comprehensive,
Cengage Learning, 2018 by Joyce Farrell
Reference Materials:
• Starting Out with Programming Logic and
Design, Pearson Education, 2015 by Tony Gaddis
THANK YOU
For your time!

FACULTY OF COMPUTING

You might also like