Chapter 5: Repetition Structures

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

While loops.
CS0007: Introduction to Computer Programming
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
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
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
© 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 …
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
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.
Chapter 4: Loops and Files. The Increment and Decrement Operators  There are numerous times where a variable must simply be incremented or decremented.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
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 © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
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.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
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
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
The switch Statement, and Introduction to Looping
Topics Introduction to Repetition Structures
Programming Logic and Design Fourth Edition, Comprehensive
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: Looping Starting Out with C++ Early Objects Seventh Edition
JavaScript: Control Statements I
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Topics Introduction to Repetition Structures
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
MSIS 655 Advanced Business Applications Programming
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Chapter 4: Loops and Files
Understanding the Three Basic Structures
Topics Introduction to Repetition Structures
Chapter 8: More on the Repetition Structure
Three Special Structures – Case, Do While, and Do Until
Chapter 6: Repetition Statements
Introduction to Repetition Structures
More Loops Topics Counter-Controlled (Definite) Repetition
Topics Introduction to Repetition Structures
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
CprE 185: Intro to Problem Solving (using C)
Presentation transcript:

Chapter 5: Repetition Structures Starting Out with Programming Logic & Design Third Edition by Tony Gaddis

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

5.1 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

5.2 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

5.2 Condition-Controlled Loops The While Loop – pretest loop While condition Statement End While Figure 5-1 The logic of a While loop

5.2 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-4

5.2 Condition-Controlled Loops The Do-While Loop – posttest loop Do Statement While condition Figure 5-8 Flowchart for the main module in Program 5-5

5.2 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

5.3 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

5.3 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

5.3 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

5.4 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

5.5 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

5.6 Nested Loops All loops can be nested, that is, a loop inside of a loop Figure 5-21 Flowchart for a clock simulator