An Introduction to Programming with C++ Fifth Edition

Slides:



Advertisements
Similar presentations
Chapter 6: The Repetition Structure
Advertisements

Programming with Microsoft Visual Basic 2008 Fourth Edition
Programming with Microsoft Visual Basic th Edition
Programming Logic and Design Eighth Edition
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
CS0004: Introduction to Programming Repetition – Do Loops.
Repeating Actions While and For Loops
1 Loops zDo While Loop: yRepeats a block of code WHILE a condition remains TRUE Do While condition Code to execute Loop.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
An Introduction to Programming with C++ Fifth Edition Chapter 10 Void Functions.
An Introduction to Programming with C++ Fifth Edition Chapter 6 More on the Selection Structure.
An Introduction to Programming with C++ Fifth Edition Chapter 8 More on the Repetition Structure.
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Six Repeating Program Instructions.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
CIS 115 Lecture 8. There are 3 control structures common to most computer languages that determine the flow, or path of execution, of the code:  Sequential.
Chapter 12: How Long Can This Go On?
Programming Logic and Design Fifth Edition, Comprehensive
Repetition & Loops. One of the BIG advantages of a computer: ­It can perform tasks over and over again, without getting bored or making mistakes (assuming.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 6: The Repetition Structure
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Six The Do Loop and List Boxes.
Chapter 4 Looping Statements Adapted From: Starting Out with Visual Basic 2008 (Pearson)
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
Computer Programming TCP1224 Chapter 8 More On Repetition Structure.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
Tutorial 6: The Repetition Structure1 Tutorial 6 The Repetition Structure.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Computer Programming 12 Lesson 6 – Loop structure By: Dan Lunney.
CHAPTER 6: REPETITION AND LOOP STATEMENTS Learning outcomes  Define the concept of repetition structure.  Specify.
An Introduction to Programming with C++ Sixth Edition Chapter 8 More on the Repetition Structure.
REPETITION CONTROL STRUCTURE
CHAPTER 6: REPETITION AND LOOP STATEMENTS
Programming Logic and Design Fourth Edition, Comprehensive
Chapter 5: Repetition Structures
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Control Structure Senior Lecturer
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Chapter 8: More on the Repetition Structure
Based on slides created by Bjarne Stroustrup & Tony Gaddis
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Presentation transcript:

An Introduction to Programming with C++ Fifth Edition Chapter 7 The Repetition Structure

Objectives Include the repetition structure in pseudocode Include the repetition structure in a flowchart Code a pretest loop using the C++ while statement Initialize and update counters and accumulators Code a pretest loop using the C++ for statement An Introduction to Programming with C++, Fifth Edition

Concept Lesson Using the Repetition Structure Pretest Loops Using the while Statement to Code a Pretest Loop An Introduction to Programming with C++, Fifth Edition

Concept Lesson (continued) Using Counters and Accumulators Counter-Controlled Pretest Loops Using the for Statement to Code a Pretest Loop An Introduction to Programming with C++, Fifth Edition

Using the Repetition Structure Repetition structure repeatedly processes (a) program instruction(s) until a condition is met Also called loop Pretest loop (also called top-driven) Condition evaluated before instructions are processed Instructions may never be processed Posttest loop (also called bottom-driven) Condition evaluated after instructions are processed Instructions within loop are always processed Most common type An Introduction to Programming with C++, Fifth Edition

Pretest Loops Not every problem requires a loop in its solution Most loops have a condition and a body Loop condition determines the number of times instructions within loop are processed In pretest loops, condition appears at the beginning Must result in true or false value only An Introduction to Programming with C++, Fifth Edition

Pretest Loops (continued) An Introduction to Programming with C++, Fifth Edition

Pretest Loops (continued) An Introduction to Programming with C++, Fifth Edition

Pretest Loops (continued) An Introduction to Programming with C++, Fifth Edition

Flowcharting a Pretest Loop An Introduction to Programming with C++, Fifth Edition

Flowcharting a Pretest Loop (continued) An Introduction to Programming with C++, Fifth Edition

Using the while Statement to Code a Pretest Loop An Introduction to Programming with C++, Fifth Edition

Using the while Statement to Code a Pretest Loop (continued) An Introduction to Programming with C++, Fifth Edition

Pretest Loop Example – O’Donnell Incorporated Program An Introduction to Programming with C++, Fifth Edition

Pretest Loop Example – O’Donnell Incorporated Program (continued) Forgetting this statement turns loop into an endless or infinite loop An Introduction to Programming with C++, Fifth Edition

Pretest Loop Example – O’Donnell Incorporated Program (continued) An Introduction to Programming with C++, Fifth Edition

Using Counters and Accumulators Counter: variable used for counting something E.g., number of employees paid in a week Accumulator: variable used for accumulating E.g., total dollar amount of a week’s payroll Initializing: assigning a beginning value to counter or accumulator Typically zero Updating (incrementing): adding a number to counter or accumulator An Introduction to Programming with C++, Fifth Edition

Counter and Accumulator Examples – Sales Express Program An Introduction to Programming with C++, Fifth Edition

Counter and Accumulator Examples – Sales Express Program (continued) An Introduction to Programming with C++, Fifth Edition

Counter and Accumulator Examples – Sales Express Program (continued) An Introduction to Programming with C++, Fifth Edition

Counter and Accumulator Examples – Sales Express Program (continued) An Introduction to Programming with C++, Fifth Edition

Counter-Controlled Pretest Loops In previous programs, user controls loop termination by entering a sentinel value Program can also control the termination of loop Typically, done using a counter Counter-controlled loop Example: Jasper Music Company program An Introduction to Programming with C++, Fifth Edition

Counter-Controlled Loop Example – Jasper Music Company Program An Introduction to Programming with C++, Fifth Edition

Counter-Controlled Loop Example – Jasper Music Company Program (continued) An Introduction to Programming with C++, Fifth Edition

Counter-Controlled Loop Example – Jasper Music Company Program (continued) An Introduction to Programming with C++, Fifth Edition

Using the for Statement to Code a Pretest Loop An Introduction to Programming with C++, Fifth Edition

Using the for Statement to Code a Pretest Loop (continued) An Introduction to Programming with C++, Fifth Edition

Using the for Statement to Code a Pretest Loop (continued) An Introduction to Programming with C++, Fifth Edition

for Statement Example 1 – Holmes Supply Program An Introduction to Programming with C++, Fifth Edition

for Statement Example 1 – Holmes Supply Program (continued) An Introduction to Programming with C++, Fifth Edition

for Statement Example 1 – Holmes Supply Program (continued) An Introduction to Programming with C++, Fifth Edition

for Statement Example 2 – Colfax Sales Program An Introduction to Programming with C++, Fifth Edition

for Statement Example 2 – Colfax Sales Program (continued) An Introduction to Programming with C++, Fifth Edition

for Statement Example 2 – Colfax Sales Program (continued) An Introduction to Programming with C++, Fifth Edition

for Statement Example 3 – Morgan Company Program An Introduction to Programming with C++, Fifth Edition

for Statement Example 3 – Morgan Company Program (continued) An Introduction to Programming with C++, Fifth Edition

for Statement Example 3 – Morgan Company Program (continued) An Introduction to Programming with C++, Fifth Edition

Summary Use repetition structure (loop) to repeatedly process program instruction(s) until condition is met Pretest or posttest Flowchart symbol is repetition/selection diamond Most loops have a condition and a body Sentinel value may be entered by user to end loop Priming read: input instruction above a pretest loop Counters and accumulators within loops used to calculate subtotals, totals, and averages An Introduction to Programming with C++, Fifth Edition

Application Lesson: Using the Repetition Structure in a C++ Program Lab 7.1: Stop and Analyze Lab 7.2 Create, test and verify the Professor Krelina program Lab 7.3 Modify the program created in Lab 7.2 Should allow user to enter four project scores and two test scores only Lab 7.4: Desk-Check Lab Lab 7.5: Debugging Lab An Introduction to Programming with C++, Fifth Edition