1 Chapter 5 File Objects and Looping Statements (Some slides have been modified from their original format)

Slides:



Advertisements
Similar presentations
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Advertisements

Repetition Chapter 4: Control structures. Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 2 Loop Statements After reading and studying this Section,
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
1 Lecture 15 Chapter 6 Looping Dale/Weems/Headington.
Chapter 6 Loops and Files. 2 Knowledge Goals Understand the semantics of while loop Understand when a count-controlled loop is appropriate Understand.
1 Lecture 16 Chapter 6 Looping Dale/Weems/Headington.
Chapter 5: Loops and Files.
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
Chapter 6 Looping Dale/Weems. 2 Chapter 6 Topics l While Statement Syntax l Count-Controlled Loops l Event-Controlled Loops l Using the End-of-File Condition.
1 Chapter 5 File Objects and Looping Statements. 2 Chapter 9 Topics l Using Data Files for I/O l While Statement Syntax l Count-Controlled Loops l Event-Controlled.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Chapter 6 Looping.
Chapter 5: Control Structures II (Repetition)
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
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.
Chapter 6 Looping CS185/09 - Introduction to Programming Caldwell College.
Chapter 6 Looping. 2 Chapter 6 Topics l While Statement Syntax l Count-Controlled Loops l Event-Controlled Loops l Using the End-of-File Condition to.
1 Looping. 2 Chapter 6 Topics  While Statement Syntax  Phases of Loop Execution  Two Types of Loops: Count-Controlled Loops &Event-Controlled Loops.
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.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 5: Control Structures II
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
Control Structures II: Repetition.  Learn about repetition (looping) control structures  Explore how to construct and use count-controlled, sentinel-controlled,
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
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.
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.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
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 6. Loops A control structure that causes a statement or group of statements to be executed repeatedly There are 3 types of loops –while –for –do.
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?
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Chapter 6 Loops and Files
Chapter 4 Repetition Statements (loops)
Chapter 6 Looping.
Chapter 5: Control Structures II
Loop Structures.
CiS 260: App Dev I Chapter 4: Control Structures II.
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Loops A loop is a repetition control structure.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Presentation transcript:

1 Chapter 5 File Objects and Looping Statements (Some slides have been modified from their original format)

2 Two types of loops count-controlled loops repeat a specified number of times event-controlled loops something happens inside the loop body that causes the repetition to stop

3 While Statement while ( Expression ) {.. // loop body. } NOTE: Loop body can be a single statement, a null statement, or a block

4 When the expression is tested and found to be false, the loop is exited and control passes to the statement that follows the loop body WHILE LOOP false true body statement Expression

5 int count; // Declare loop variable count = 1;// Initialize loop variable while (count <= 4)// Test expression { // Repeated action (iterations) System.out.println(“count is “ + count); count ++; // Update loop variable } System.out.println(“Done”); Count-controlled Example

6 Count-controlled loop int count; count = 1; while ( count <= 4 ) { System.out.println ( “count is “ + count ); count++; } System.out.println(“Done”); OUTPUT count

7 See step-by-step execution of count-controlled loop in separate file, loop.ppt.

8 dataFile contains 100 blood pressures, one to a line Use a while loop to read the 100 blood pressures and find their total Count-Controlled Loop Example

9 // Count-controlled loop int thisBP; int total; int count; count = 1;// Initialize total = 0; while (count <= 100)// Test expression { thisBP = Integer.parseInt(dataFile.readLine()); total = total + thisBP; count++; // Update } System.out.println(“The total = “ + total);

10 Event-controlled loops l Sentinel controlled Keep processing data until a special value which is not a possible data value is entered to indicate that processing should stop l End-of-file controlled Keep processing data as long as there is more data in the file l Flag controlled Keep processing data until the value of a flag changes in the loop body

11 Examples of kinds of loops What kind of loop ?Read exactly 100 blood pressures from a file. What kind of loop ? Read all the blood pressures from a file no matter how many are there. What kind of loop ? Read blood pressures until a dangerously high BP (200 or more) is read. What kind of loop ? Keep reading until a special (impossible) value is read.

12 Examples of kinds of loops Count-controlled loop Read exactly 100 blood pressures from a file. End-of-file controlled loop Read all the blood pressures from a file no matter how many are there. Flag-controlled loop Read blood pressures until a dangerously high BP (200 or more) is read. Sentinel-controlled loop Keep reading until a special (impossible) value is read.

13 // Sentinel is negative blood pressure. int thisBP; int total; int count; count = 1;// Initialize total = 0; // Priming read thisBP = Integer.parseInt(dataFile.readLine()); while (thisBP > 0)// Test expression { total = total + thisBP; count++; // Update thisBP = Integer.parseInt(dataFile.readLine()); } System.out.println(“The total = “ + total); A sentinel-controlled loop

14 // Read and sum until end of line int thisBP; int total; int count; count = 1;// Initialize total = 0; String line; line = dataFile.readLine(); while (line != null)// Test expression { thisBP = Integer.parseInt(line); total = total + thisBP; count++; // Update line = dataFile.readLine(); } System.out.println(“The total = “ + total); An end-of-file controlled loop

15 A flag-controlled loop

16 count = 0; sum = 0; notDone = true; while ( notDone ) { line = dataFile.readLine( ); // Get a line if (line != null) // Got a line? { number = Integer.parseInt(line); if (number % 2 == 1) // Is number odd? { count++; sum = sum + number; notDone = ( count < 10 ); } else // Reached EOF unexpectedly { errorFile.println(“EOF reached before ten odd values.”) notDone = false; // Change flag value }

17 initialize outer loop while ( outer loop condition ) {... initialize inner loop while ( inner loop condition ) { inner loop processing and update }... } Pattern of a nested loop

18 Loop Testing and Debugging Test data should test all sections of the program l Beware of infinite loops -- the program doesn’t stop l Check loop termination condition, and watch for an OBOB (off-by-1 bug) l Use algorithm walk-through to verify that appropriate conditions occur in the right places l Trace execution of loop by hand with code walk-through l Use a debugger (if available) to run program in “slow motion” or use debug output statements