Neal Stublen Loop Structure  Sometimes it’s useful to repeat yourself  The same actions need to occur more than once  Ensures.

Slides:



Advertisements
Similar presentations
Programming Logic and Design Eighth Edition
Advertisements

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.
Programming Logic and Design, Third Edition Comprehensive
Objectives In this chapter, you will learn about:
James Tam Loops In Python In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
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.
Chapter 4 Repetitive Execution. 2 Types of Repetition There are two basic types of repetition: 1) Repetition controlled by a counter; The body of the.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
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.
Repetition. Examples When is repetition necessary/useful?
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
An Object-Oriented Approach to Programming Logic and Design Chapter 6 Looping.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Fundamentals of Python: From First Programs Through Data Structures
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Fundamentals of Python: First Programs
Programming Logic and Design Fifth Edition, Comprehensive
Programming Logic and Design Sixth Edition Chapter 5 Looping.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
1 Loops. 2 Topics The while Loop Program Versatility Sentinel Values and Priming Reads Checking User Input Using a while Loop Counter-Controlled (Definite)
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
Repetition Control Structures Simple Program Design Third Edition A Step-by-Step Approach 5.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
Loops George Mason University. Loop Structure Loop- A structure that allows repeated execution of a block of statements Loop body- A block of statements;
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 5 Repetition.
Loops and Files. 5.1 The Increment and Decrement Operators.
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.
Chapter 4 Introduction to Control Statements
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
9. ITERATIONS AND LOOP STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Chapter 6: Looping. Objectives Learn about the loop structure Create while loops Use shortcut arithmetic operators Create for loops Create do…while loops.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
1 Looping Chapter 6 2 Getting Looped in C++ Using flags to control a while statement Trapping for valid input Ending a loop with End Of File condition.
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.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
REPETITION CONTROL STRUCTURE
Programming Logic and Design Fourth Edition, Comprehensive
Loop Structures.
CHAPTER 5A Loop Structure
Topics Introduction to Repetition Structures
Control Structure Senior Lecturer
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Lesson #5 Repetition and Loops.
CS150 Introduction to Computer Science 1
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Week 6 CPS125.
Let’s all Repeat Together
Alternate Version of STARTING OUT WITH C++ 4th Edition
CHAPTER 4 Iterative Structure.
Topics Introduction to Repetition Structures
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Loops.
While Loops in Python.
Iteration – While Loops
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Neal Stublen

Loop Structure  Sometimes it’s useful to repeat yourself  The same actions need to occur more than once  Ensures that each repetition is done in exactly the same way  Examples?

Example Loops while book is not finished read another page endwhile while thirsty drink water endwhile

Loop Control Variables  Usually, we want to continue looping until some condition is satisfied  The condition is represented by a loop control variable Initialized before entering the loop Test control variable with each iteration of the loop Take some action in the body of the loop that may update the loop control variable

Example Loops // Definite loop – fixed number of iterations num pagesLeft = 100 while pagesLeft > 0 read another page pagesLeft = pagesLeft - 1 endwhile // Indefinite loop – unknown number of iterations while sick take medicine sick = is patient feverish? endwhile

Nested Loops  Loops can be “nested” so one loop occurs within another loop  Examples? Spreadsheet tables have rows and columns Smartphone home screen has pages, rows, and columns

Example Nested Loops num reports = number of reports while reports > 0 num pagesLeft = number of pages in report while pagesLeft > 0 print another page pagesLeft = pagesLeft - 1 endwhile reports = reports - 1 endwhile

Common Loop Mistakes  Infinite loops The condition that terminates the loop is missing or incorrect Failure to initialize the control variable? Failure to update the control variable? Failure to check the control variable correctly?

Loop Inefficiency user = 1 while user <= lookup number of users print “User “ + user + " of “ + lookup number of users print user information endwhile user = 1 totalUsers = lookup number of users while user <= totalUsers print “User “ + user + " of “ + totalUsers print user information endwhile

Thermostat Logic  What logic would we need to implement a thermostat that controls a home furnace?  What if we also wanted to control the air conditioner?

Using for Loops count = 0 while count < 10 take some action count = count + 2 endwhile for count = 0; count < 10; count += 2 take some action endfor

for Loops Explained for count = 0; count < 10; count += 2 take some action endfor The loop initializes the control variable.

for Loops Explained for count = 0; count < 10; count += 2 take some action endfor The loop checks for a terminating condition.

for Loops Explained for count = 0; count < 10; count += 2 take some action endfor The loop modifies the control variable with each iteration.

for Loops Explained for count = 8; count >= 0; count -= 2 take some action endfor The initial value, condition, and step can also go backwards.

Common Loop Applications

 Accumulate totals Sum numeric values in a column from a spreadsheet  Validate data Importing records from a file, values can be validated to make sure they fit within an expected range Keep prompting for the same user input if it was not valid the first time

Summary  Loop structure  Loop control variables  Nested loops  Common loop mistakes  Using for loops  Common loop applications

Guess a Number  What logic is necessary to have a user guess a random number between 1 and 100?  After each guess, the user will be told if his guess was correct, too high, or too low.  After seven guesses, the user has failed at the task.