chapter 1: an introduction to control structures introduction to programming with c++ fourth edition

12
Chapter 1: An Introduction to Control Structures Introduction to Programming with C++ Fourth Edition

Upload: rosemary-black

Post on 04-Jan-2016

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Chapter 1: An Introduction to Control Structures Introduction to Programming with C++ Fourth Edition

Chapter 1: An Introduction to Control

Structures

Introduction to Programming with C++

Fourth Edition

Page 2: 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

Page 3: Chapter 1: An Introduction to Control Structures Introduction to Programming with C++ Fourth Edition

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

Page 4: Chapter 1: An Introduction to Control Structures Introduction to Programming with C++ Fourth Edition

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)

Page 5: Chapter 1: An Introduction to Control Structures Introduction to Programming with C++ Fourth Edition

Introduction to Programming with C++, Fourth Edition 5

The Sequence Structure

Page 6: Chapter 1: An Introduction to Control Structures Introduction to Programming with C++ Fourth Edition

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

Page 7: Chapter 1: An Introduction to Control Structures Introduction to Programming with C++ Fourth Edition

Introduction to Programming with C++, Fourth Edition 7

The Repetition Structure

Page 8: Chapter 1: An Introduction to Control Structures Introduction to Programming with C++ Fourth Edition

Introduction to Programming with C++, Fourth Edition 8

The Repetition Structure

Page 9: Chapter 1: An Introduction to Control Structures Introduction to Programming with C++ Fourth Edition

Introduction to Programming with C++, Fourth Edition 9

Rob’s New Instruction Set

Page 10: Chapter 1: An Introduction to Control Structures Introduction to Programming with C++ Fourth Edition

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

Page 11: Chapter 1: An Introduction to Control Structures Introduction to Programming with C++ Fourth Edition

Introduction to Programming with C++, Fourth Edition 11

The Selection Structure (continued)

Page 12: Chapter 1: An Introduction to Control Structures Introduction to Programming with C++ Fourth Edition

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