CS 117 Spring 2002 Repetition Hanly Chapter 4 Friedman-Koffman Chapter 5.

Slides:



Advertisements
Similar presentations
Computer Science 1620 Loops.
Advertisements

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 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Chapter 5: Control Structures II (Repetition)
©2004 Brooks/Cole Chapter 5 Repetition. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Repetition So far, our programs processed a single set.
Loops – While, Do, For Repetition Statements Introduction to Arrays
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Chapter 4: Looping. Resource: Starting Out with C++, Third Edition, Tony Gaddis 5.1 The Increment and Decrement Operators ++ and -- are operators that.
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
Repetition and Loop Statements Chapter 5. 2 Why iterate? t Example: Algorithm for searching a letter w in a string that consists of n letters 1. Extract.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 6 Looping CS185/09 - Introduction to Programming Caldwell College.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
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.
Fundamental Programming Fundamental Programming for Loops.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Overview Go over parts of quiz? Another iteration structure for loop.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
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.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
CISC105 – General Computer Science Class 4 – 06/14/2006.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
Chapter 6 Looping. 2 l A loop is a repetition control structure. l it causes a single statement or block to be executed repeatedly What is a loop?
Computer Programming -1-
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
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Programming Fundamentals
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Lesson #5 Repetition and Loops.
Repetition Control Structure
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Presentation transcript:

CS 117 Spring 2002 Repetition Hanly Chapter 4 Friedman-Koffman Chapter 5

Flow Control Three types of program flow –sequential (what we did first) –selection (what we just looked at) if - else switch –repetition (Chapter 4) while do - while for

Repetition There are many situations where you need to repeat the same actions over and over. –averaging a group of numbers –reading a file one line at a time –searching through a group of objects –repeating a calculation for different input values

Why iterate? t Use the computer's speed to do the same task faster than if done by hand. t Avoid writing the same statements over and over again.

Repetitive control structures –Because many algorithms require many iterations over the same statements. To average 100 numbers, we would need 300 plus statements. Or we could use a statement that has the ability to repeat a collection of statements:

Sum 100 values the hard way int sum = 0; cout << "Enter number: "; // <-Repeat these three cin >> number; // <- statements for each sum = sum + number; // <- number in the set cout << "Enter number: "; cin >> number; sum = sum + number;

Sum 100 values the hard way /* statements deleted... */ cout << "Enter number: "; cin >> number; sum = sum + number; average = sum / 100;

2/15/02 Questions about program 2 Holiday Monday - building may be closed seminar Wed Feb 20 using namespace

Loop Behavior

while loop Basic syntax while (cond) doSomething; As in the if statement, the first statement after the while is considered to be the body of the loop. Use { } to make it more than one statement. If the condition is false to begin with, the loop doesn’t execute at all.

Sentinel Loop Useful for finding the end of a list of numbers if you know all valid data is positive, stop the loop when a negative value is input double data = 1.0 while (data > 0.0) { cin >> data; processData; }

readme your name, assignment instructions for using the program anything you did to try to get extra credit what you liked, disliked, had particular trouble with what you would like to do to make the program better

while loop Basic syntax while (cond) doSomething; As in the if statement, the first statement after the while is considered to be the body of the loop. Use { } to make it more than one statement. If the condition is false to begin with, the loop doesn’t execute at all.

5.4 Conditional Loops t In many programming situations, you will not be able to determine the exact number of loop repetitions t Conditional Loop –Initialize the loop control variable –While a condition involving the loop control variable is true – Continue processing – Update the loop control variable

Input loop reading data until the input ends while (!cin.eof()) { cin >> data; processData; }

Flag-Controlled Loops char getDigit() { char nextChar; bool digitRead; digitRead = false; while (!digitRead) { cin >> nextChar; digitRead = (('0' <= nextChar) && (nextChar <= '9')); } return nextChar; }

Counting Loop a loop that repeats a pre-defined number of times int count = 0, maxCount=10; while (count<maxCount) { doWhatever; count = count +1; }

for loop Counting loop so common there is a special loop statement to handle it. for (init; cond; update) forBody; –init initializes the loop control variable –cond is the loop repetition condition –update updates the loop control variable

counting loop with for The counting loop from above becomes for (count=0; count<10; count=count+1) doWhatever;

A different loop order

do-while loop like while except loop control variable is checked after the body has executed char ch; do { mainAction; cout << "Enter a character, q to quit "; cin >> ch; } while (ch!='q'); the loop body always executes at least once.

loop cautions while (cond); –has an empty body. –Infinite loops: control variable doesn’t get changed so loop repeats forever

2/20/02 Friday - last drop date

Program 3 –Menu loop –Selection to select requested action converting between bearing and heading –Alternate version slightly more difficult

Program 3 Heading –N=0 = 360 –S = 180 –E = 90 –W = 270 Bearing –face north or south –degrees (0-90) to turn from that direction –direction to turn - either east or west

Loop Review :while Most commonly used when repetition is not counter controlled condition test precedes each loop repetition loop body may not be executed at all while (condition) dothis;

Loop Review: do-while Convenient when at least one repetition of loop body must be ensured test condition after execution of body do that; while (condition);

Loop Overview Counting loop - number of repetitions –known ahead of time –can be controlled by a counter also convenient for loops involving non counting loop control with simple initialization and updates condition test precedes the execution for (initialization; condition; modification) doSomething;

5.8 Nested Loops t Possible to nest loops –Loops inside other loops –Start with outer jump to inner loop –Inner loop continues until complete –Back to outer loop and start again t NestLoop.cpp and Triangle.cpp examples

Nested Logic outer = 1; while(outer <= 3) { inner = 1; // Resets for each iteration of the outer loop while(inner <= outer) { cout << outer << " " << inner << endl; inner = inner + 1; } // End of the nested loop outer = outer + 1; // Increment the outer loop counter } // The end of the outer loop

increment and decrement operators For looping operations, you will frequently need to increment or decrement by 1. C++ has special operators, ++ and -- to do this. –i++; is equivalent to i = i + 1; –j--;is equivalent to j = j - 1;

Compound Assignment Operators t Lets look at the idea of adding together a group of numbers t Short hand notation totalPay += pay; –same as totalPay = totalPay + pay; t See Table 5.2