chapter 1: an introduction to control structures introduction to programming with c++ fourth edition
TRANSCRIPT
Chapter 1: An Introduction to Control
Structures
Introduction to Programming with C++
Fourth Edition
Introduction to Programming with C++, Fourth Edition 2
Objectives
• Explain the sequence, selection, and repetition structures
• Write simple algorithms using the sequence, selection, and repetition structures
Introduction to Programming with C++, Fourth Edition 3
Defining Control Structures
• All computer programs, no matter how simple or how complex, are written using one or more of three basic structures:– Sequence
– Selection
– Repetition
Introduction to Programming with C++, Fourth Edition 4
The Sequence Structure
• Directs the computer to process the program instructions, one after another, in the order listed in the program
• Rob, the mechanical man, has a limited instruction set (walk, turn, and sit)
Introduction to Programming with C++, Fourth Edition 5
The Sequence Structure
Introduction to Programming with C++, Fourth Edition 6
The Repetition Structure
• Makes a decision, then takes appropriate action based on that decision
– Example: shampoo bottles typically include the repetition structure in the directions for washing your hair
Introduction to Programming with C++, Fourth Edition 7
The Repetition Structure
Introduction to Programming with C++, Fourth Edition 8
The Repetition Structure
Introduction to Programming with C++, Fourth Edition 9
Rob’s New Instruction Set
Introduction to Programming with C++, Fourth Edition 10
The Selection Structure
• Directs the computer to repeat one or more instructions until some condition is met, at which time the computer should stop repeating the instructions
– Example: Selection structure is used every time you drive your car and approach an intersection
Introduction to Programming with C++, Fourth Edition 11
The Selection Structure (continued)
Introduction to Programming with C++, Fourth Edition 12
Summary
• Basic components of all algorithmic problem solutions:– Sequence
• Process instructions one after another– Selection
• Take an action based on a decision– Repetition
• Repeat a set of instructions until a condition is met
• Write simple algorithms using the sequence, selection, and repetition structures