Chapter 3 Understanding Structure SLIDES
Chapter 3 Understanding Structure SLIDES
Ninth Edition
Chapter 3
Understanding Structure
•
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
• 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
• Dual-alternative ifs
– Contains two alternatives
– The if-then-else structure
• Single-alternative ifs
• 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
• 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