Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.

Slides:



Advertisements
Similar presentations
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.
Advertisements

CS0007: Introduction to Computer Programming
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Introduction to Flowcharting
Introduction to Flowcharting
Introduction to Flowcharting A Supplement to Starting Out with C++, 4th Edition by Tony Gaddis Published by Addison-Wesley.
CS0004: Introduction to Programming Repetition – Do Loops.
LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
Repeating Actions While and For Loops
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.
Chapter 5: Loops and Files.
An Introduction to Programming with C++ Fifth Edition
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
1 Fall 2008ACS-1903 Ch 4 Loops and Files while loop do-while loop Other topics later on …
1 Fall 2007ACS-1903 Ch 4 Loops and Files Increment & decrement statements while loop do-while loop Other topics later on …
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Part 1 Conditionals and Loops.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
(C)opyright 2003 Scott/Jones Publishers Introduction to Flowcharting A Supplement to Starting Out with C++, 4th Edition by Tony Gaddis Scott/Jones Publishers.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Looping Exercises Deciding Which Loop to Use At this.
Chapter 4: Looping. Resource: Starting Out with C++, Third Edition, Tony Gaddis 5.1 The Increment and Decrement Operators ++ and -- are operators that.
Intro to Programming Logic and Design CIS 115 Brought to you by: done by Angela Robinson for CSC 289.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Programming Logic and Design Fifth Edition, Comprehensive
Chapter 4: Loops and Files
Chapter 4: Loops and Files. The Increment and Decrement Operators  There are numerous times where a variable must simply be incremented or decremented.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
CS 100 Introduction to Computing Seminar October 7, 2015.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 5 Repetition.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
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.
Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 4 Slide #1.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
Programming Logic and Design, Introductory, Fourth Edition1 Understanding the Three Basic Structures Structure: a basic unit of programming logic Any program.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
1 Fall 2009ACS-1903 Ch 4 Loops and Files while loop do-while loop for loop Other topics later on …
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING For Loop.
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
Chapter 5: Looping. Using the while Loop Loop – A structure that allows repeated execution of a block of statements Loop body – A block of statements.
Programming Logic and Design Fourth Edition, Introductory Chapter 2 Understanding Structure.
REPETITION CONTROL STRUCTURE
Topics Introduction to Repetition Structures
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Chapter 5: Repetition Structures
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Topics Introduction to Repetition Structures
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Chapter 8: More on the Repetition Structure
Three Special Structures – Case, Do While, and Do Until
Topics Introduction to Repetition Structures
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Presentation transcript:

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis Chapter 5: Repetition Structures

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-2 Chapter Topics 5.1 Introduction to Repetition Structures 5.2 Condition-Controlled Loops: While, Do- While, and Do-Until 5.3 Count-Controlled Loops and the For Statement 5.4 Calculating a Running Total 5.5 Sentinels 5.6 Nested Loops

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Introduction to Repetition Structures A repetition structure causes a statement or set of statements to execute repeatedly Allow a programmer to avoid duplicate code –Duplicate code makes a program large –Write a long sequence of statements is time consuming –If part of the duplicate code has to be corrected or changed, then the change has to be done many times

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Condition-Controlled Loops While Loop –While a condition is true, do some task Do-While Loop –Do some task, while condition is true Do-Until Loop –Do some task, while a condition is false (or until it’s true) With all loops, be careful not to create infinite loops – always provide a way to break out

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Condition-Controlled Loops The While Loop – pretest loop While condition Statement End While Figure 5-1 The logic of a While loop

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Condition-Controlled Loops Working with Modules and Loops To run a program multiple times, modules can be put within a loop Figure 5-5 The main module of Program 5-3

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Condition-Controlled Loops The Do-While Loop – posttest loop Do Statement While condition Figure 5-8 Flowchart for the main module in Program 5-4

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Condition-Controlled Loops The Do-Until Loop Loop iterates until a condition is true, but not all languages support this type of loop Figure 5-10 The logic of a Do-Until loop

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Count-Controlled Loops A count-controlled loop iterates a specific number of times A for loop is best used for this situation For counterVariable = startingValue to maxValue statement End for There is an Initialization, Test, and Increment expression that controls the loop

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Count-Controlled Loops For loops can also increment by more than one, count backwards by decrementing, or allow the user to control the number of interactions The for loop in action Figure 5-14 Flowchart for Program 5-8

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Count-Controlled Loops General loop concerns Do not forget to initialize the loop control variable Do not forget to modify the loop control variable Many loops are interchangeable, but generally –Use while loop when loop may not have to process –Use do while when it must process at least once –Use for loop with specific number of iterations

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Calculating a Running Total A running total is a sum of number that accumulates with each iteration of a loop Figure 5-19 Flowchart for Program 5-18

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Sentinels A sentinel is a special value that marks the end of a list of values, used as stop values for loops How it can be done –Ask the user at the end of each loop iteration, if there is another value to process –Ask the user at the beginning of the loop, how many times the loop should process

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Nested Loops All loops can be nested, that is, a loop inside of a loop Figure 5-21 Flowchart for a clock simulator