“Here we go Loop de Loo” (Looping) So far you have learned (I hope) some decision making C++ control structures: *if (condition) else *if (condition) else.

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Repetition – Do Loops.
Advertisements

Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
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.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Chapter 5: Loops and Files.
Sahar Mosleh California State University San MarcosPage 1 While Loop and For Loop.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
CS 1 Lesson 5 Loops and Files CS 1 -- John Cole.
1 Fall 2008ACS-1903 Ch 4 Loops and Files while loop do-while loop Other topics later on …
1 Chapter 5 File Objects and Looping Statements (Some slides have been modified from their original format)
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Chapter 5: Control Structures II (Repetition)
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
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.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Chapter 6 Looping CS185/09 - Introduction to Programming Caldwell College.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
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.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
Control Structures RepetitionorIterationorLooping Part I.
Loops and Files. 5.1 The Increment and Decrement Operators.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
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.
Looping ROBERT REVAES. Logical Operators  && AND  Both have to be true for it to evaluate to be true.  || OR  One or the other has to be true for.
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 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.
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?
Loop. 3.1Introduction to Repetition Structures Loop – a block of code that, under certain conditions will be executed repeatedly. Do Prompt for and input.
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.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Lesson #5 Repetition and Loops.
REPETITION CONTROL STRUCTURE
Lesson #5 Repetition and Loops.
Chapter 5: Loops and Files.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
( Iteration / Repetition / Looping )
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Topics Introduction to Repetition Structures
While Loops Chapter 3.
Chapter 8 Repetition Statements
Lesson #5 Repetition and Loops.
Alternate Version of STARTING OUT WITH C++ 4th Edition
Control Structures Part 1
Let’s all Repeat Together
ICT Programming Lesson 3:
Topics Introduction to Repetition Structures
Lesson #5 Repetition and Loops.
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Presentation transcript:

“Here we go Loop de Loo” (Looping) So far you have learned (I hope) some decision making C++ control structures: *if (condition) else *if (condition) else if (condition) … else *switch Now we will learn some ‘looping’ or repeating control structures

First of Three Looping Control Structures while Loop while (continue_condition) { Statements to execute as long as continue_condition is true } Ex. int x, //loop control variable y; //used to output odd numbers less than 20 x = 0; //initialize because it must have a known //value prior to entering the loop while (x < 20) { y = x % 2; if (y == 1) cout << y << endl; x = x + 1; }

Reading Data from a File Using a Sentinel for End-of-Data One way to indicate the end of data in an input file is to place what is called a ‘sentinel’ value at the end of the data. When the program reads in the sentinel value, it will assume that all data has been gathered and stop accessing the input file for data. The ‘while’ loop is the best control structure to use to look for the sentinel value. Below is the pseudocode for the while structure. read first value from file while (value != sentinel)//Possibly while (value within range) process data read next value from file endwhile Note: Sometimes the ‘sentinel’ may not be a single value but a range. For example, the ‘sentinel’ may be any negative value.

Looking for the EOF (End-of-File) If there is no sentinel value then the program needs to look for the end of the file. When a file stream object gets data from a file it is assigned a value of true or false indicating whether or not the read was successful. This can be used to determine the EOF. Ex. int int_data; ifstream fin; fin.open(“input.txt”); while (fin >> int_data); { }

while Loop Observations The continue test condition is tested at the beginning of the loop (called pretest) If the test condition tests false during the first iteration then the body of the while loop is NEVER executed One must be careful that any loop control variable is initialized or acquires a valid value BEFORE the loop control structure is executed. The test condition must get a new value at each iteration or you create an ‘infinite loop’ The new value must at some point be able to terminate the loop execution

Suspect an Infinite Loop if … Console output won’t stop The program seems to go away but you can’t open the output file Computer suddenly gets VERY slow Stop button on Eclipse console panel stays RED. It should gray out once a program is done executing. To stop execution of an Eclipse program hit the RED stop button! Warning: An infinite loop writing to an output file can fill up the storage unit. This can cause file damage if this is your system drive. Note: All of you, at some time in your life, are extremely likely to write a program containing an infinite loop.