0% found this document useful (0 votes)
46 views35 pages

Chapter 3 Understanding Structure SLIDES

The document discusses structured programming and its advantages over unstructured "spaghetti code". It describes the three basic structures of programming logic: sequence, selection, and loop. Programmers are advised to use these structures and techniques like priming inputs, stacking, and nesting to make code clearer, more professional, efficient and modular.

Uploaded by

Kofy Noka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views35 pages

Chapter 3 Understanding Structure SLIDES

The document discusses structured programming and its advantages over unstructured "spaghetti code". It describes the three basic structures of programming logic: sequence, selection, and loop. Programmers are advised to use these structures and techniques like priming inputs, stacking, and nesting to make code clearer, more professional, efficient and modular.

Uploaded by

Kofy Noka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Programming Logic and Design

Ninth Edition

Chapter 3
Understanding Structure

Programming Logic and Design, Ninth Edition 1


Objectives
In this chapter, you will learn about:
• The disadvantages 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

Programming Logic and Design, Ninth Edition 2


The Disadvantages of Unstructured


Spaghetti
Spaghetti code
Code
– Logically snarled program statements
– Often a complicated mess
– Programs often work but are difficult to read and maintain
– Confusing and prone to error
• Unstructured programs
– Do not follow the rules of structured logic
• Structured programs
– Follow the rules of structured logic

Programming Logic and Design, Ninth Edition 3


Unstructured Spaghetti Code

Programming Logic and Design, Ninth Edition 4


Understanding the Three Basic
Structures
• Structure
– Basic unit of programming logic
– Each structure is one of the following:
• Sequence structure
• Selection structure (decision structure)
• Loop structure
– any program can be constructed using one or more of
these three structures

Programming Logic and Design, Ninth Edition 5


Understanding the Three Basic
Structures (continued)

• Sequence structure
– Perform actions or tasks in order
– No branching or skipping any task
• Selection structure (decision structure)
– Ask a question, take one of two actions based on testing a
condition. Known as evaluating a Boolean expression, a
statement that is either true or false
– Often called if-then-else
– Dual-alternative ifs or single-alternative ifs
• Loop structure
– Repeat actions while a condition remains true

Programming Logic and Design, Ninth Edition 6


The Sequence Structure

Programming Logic and Design, Ninth Edition 7


The Selection Structure

Programming Logic and Design, Ninth Edition 8


The Selection Structure (continued -1)

• Dual-alternative ifs
– Contains two alternatives
– The if-then-else structure

if someCondition is true then


do oneProcess
else
do theOtherProcess
endif

Programming Logic and Design, Ninth Edition 9


The Selection Structure (continued -2)

• Single-alternative ifs

if employee belongs to dentalPlan then


deduct $40 from employeeGrossPay

– An else clause is not required


• null case
– Situation where nothing is done

Programming Logic and Design, Ninth Edition 10


The Selection Structure (continued -3)

Programming Logic and Design, Ninth Edition 11


The Loop Structure

• Loop structure
– Repeats a set of actions while a condition remains true
• Loop body
– Also called repetition or iteration
– Condition is tested first in the most common form of loop
– The while…do or while loop

Programming Logic and Design, Ninth Edition 12


The Loop Structure (continued -1)

Programming Logic and Design, Ninth Edition 13


The Loop Structure (continued -2)

• Loop structure
while testCondition continues to be true
do someProcess
endwhile
while you continue to be hungry
take another bite of food
determine if you still feel hungry
endwhile

Programming Logic and Design, Ninth Edition 14


Combining Structures
• All logic problems can be solved using only sequence,
selection, and loop
• Structures can be combined in an infinite number of
ways
• Stacking structures
– Attaching structures end-to-end
• End-structure statement
– Indicates the end of a structure
– The endif statement ends an if-then-else structure
– The endwhile statement ends a loop structure
Programming Logic and Design, Ninth Edition 15
Combining Structures (continued -1)

Programming Logic and Design, Ninth Edition 16


Combining Structures (continued -2)

• Any individual task or step in a structure can be


replaced by a structure
• Nesting structures
– Placing one structure within another
– Indent the nested structure’s statements
• Block
– A group of statements that execute as a single unit

Programming Logic and Design, Ninth Edition 17


Combining Structures (continued -3)

Programming Logic and Design, Ninth Edition 18


Combining Structures (continued -4)

Programming Logic and Design, Ninth Edition 19


Combining Structures (continued -5)

Programming Logic and Design, Ninth Edition 20


Combining Structures (continued -6)

Programming Logic and Design, Ninth Edition 21


Combining Structures (continued -7)

• Structured programs have the following


characteristics:
– Include only combinations of the three basic structures
– Each structure 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

Programming Logic and Design, Ninth Edition 22


Using a Priming Input to Structure
a Program
• Priming input (or priming read)
– Reads the first input data record
– Is 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

Programming Logic and Design, Ninth Edition 23


Using a Priming Input to Structure
a Program (continued -1)

Programming Logic and Design, Ninth Edition 24


Using a Priming Input to Structure
a Program (continued -2)

Programming Logic and Design, Ninth Edition 25


Using a Priming Input to Structure
a Program (continued -3)

Programming Logic and Design, Ninth Edition 26


Using a Priming Input to Structure
a Program (continued -4)

Programming Logic and Design, Ninth Edition 27


Understanding the Reasons for
Structure
• Use structured programming for:
– Clarity—unstructured programs are confusing
– Professionalism—other programmers expect it
– Efficiency—most languages support it
– Maintenance —other programmers find it easier to read
– Modularity —easily broken down into modules
• Structured programming is sometimes called goto-
less programming

Programming Logic and Design, Ninth Edition 28


Recognizing Structure

Programming Logic and Design, Ninth Edition 29


Recognizing
Structure (continued -1)

Programming Logic and Design, Ninth Edition 30


Recognizing
Structure (continued -2)

Programming Logic and Design, Ninth Edition 31


Recognizing
Structure (continued -3)

Programming Logic and Design, Ninth Edition 32


Recognizing
Structure (continued -4)

Programming Logic and Design, Ninth Edition 33


Summary
• Spaghetti code
– Statements that do not follow rules of structured logic
• Three basic structures
– Sequence, selection, and loop
– Combined by stacking and nesting
• Priming input
– Statement that reads the first input value prior to starting
a structured loop

Programming Logic and Design, Ninth Edition 34


Summary (continued)

• Structured techniques promote:


– Clarity
– Professionalism
– Efficiency
– Modularity
• Flowcharts can be made structured by untangling
logic
• Logical steps can be rewritten to conform to the
three structures: sequence, selection, and loop

Programming Logic and Design, Ninth Edition 35

You might also like