Chapter 6 Iteration Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E. Reingold.

Slides:



Advertisements
Similar presentations
Chapter 14 Recursion Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,, E. Reingold.
Advertisements

Chapter 4 Decision Making Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E. Reingold.
LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
Repeating Actions While and For Loops
June 10, 2015ICS102: while & do-while1 while and do-while Statements.
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.
Introduction to Computer Science Iteration –the while loop –the for loop Unit 8.
An Introduction to Programming with C++ Fifth Edition
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand basic loop concepts: ■ pretest loops and post-test loops ■ loop.
1 Fall 2008ACS-1903 Ch 4 Loops and Files while loop do-while loop Other topics later on …
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
©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.
Chapter 2 Classes and Methods I Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
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)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
Chapter 4: Loops and Files. The Increment and Decrement Operators  There are numerous times where a variable must simply be incremented or decremented.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
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 (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
CMSC 104, Lecture 171 More Loops Topics l Counter-Controlled (Definite) Repetition l Event-Controlled (Indefinite) Repetition l for Loops l do-while Loops.
CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
CS 100 Introduction to Computing Seminar October 7, 2015.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
1 do-while Statement 2 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX.
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.
Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 4 Slide #1.
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.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
1 Fall 2009ACS-1903 Ch 4 Loops and Files while loop do-while loop for loop Other topics later on …
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
While ( number
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Chapter 9 Nested Loops and Two-Dimensional Arrays Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin,
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
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.
Topic 4: Looping Statements
Chapter 5: Control Structures II (Repetition)
Chapter 5: Repetition Structures
Java Programming: Guided Learning with Early Objects
Chapter 6 Repetition Objectives ❏ To understand basic loop concepts:
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Chapter 8: More on the Repetition Structure
Chapter 6: Repetition Statements
Topics Introduction to Repetition Structures
Presentation transcript:

Chapter 6 Iteration Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E. Reingold

Chapter Preview In this chapter we will: discuss the use of repetition (iteration) in programming algorithms describe the Java while, for, and do-while statements demonstrate the use of loop invariants to verify the correctness of loops show the use of loops in data entry and computer animation

while Loop Repeated executes body of the loop which can be a single or compound statement A while loop will execute as long as its condition is true An infinite loop will occur if the condition never becomes false Example: count = 1; while (count <= 10) count = count + 1;

while Loop From Temperature Conversion Program OutputBox out = new OutputBox( ); out.println(“\tDEGREES C\tDEGREES F”); // initialize the loop variable cent = LOW_TEMP; // test the loop variable while (cent <= HIGH_TEMP) { // convert C to F fahr = (9.0 / 5.0) * cent ; out.println(“\t” + cent “\t\t” fahr); // increment loop variable cent = cent + 1.0; }

for Statement Used to implement pre-test loops that are controlled by incrementing a variable each time the loop is executed Loop variable computations for initialization, testing, and incrementing appear in a single statement Example: for(count=1; count <= 10; count = count + 1);

Comparing for and while Done using for for ( statement1; condition; statement2 ) statement 3; Equivalent while loop statement1; while (condition) { statement3; statement2; }

Temperature Conversion Program Loop Implemented Using for OutputBox out = new OutputBox( ); out.println(“\tDEGREES C\tDEGREES F”); for (cent = LOW_TEMP; cent <= HIGH_TEMP; cent = cent + 1.0;) { // convert C to F fahr = (9.0 / 5.0) * cent ; out.println(“\t” + cent “\t\t” fahr); }

do-while Statement Used to implement post-test loops that always execute the loop body at least one time A do-while loop will continue execute as long as its condition is true An infinite loop will occur if the condition never becomes false Example: count = 1; do { count = count + 1; } while (count <= 10);

Comparing do-while and while Done using do-while do statement; while (condition); Equivalent while loop statement; while (condition) statement;

Comparing do-while and while Done using do-while numberOfDigits = 0; rest = number; do { rest = rest / 10; numberOfDigits++; } while (rest != 0); Equivalent while loop numberOfDigits = 0; rest = number; if (number = 0) numberOfDigits = 1; else while (rest > 0) { rest = rest / 10; numberOfDigits++; };

Reading Input Using a Loop Done using do-while read score if ( not end of input) do { process score read score } while ( not end of input ); Done using while read score while ( not end of input ) { process score read score };

Sentinel Controlled Loop int i = 0; InputBox in = new InputBox(); In.setPrompt(“Enter data or –1 to terminate”); // read first score score = in.readInt(); while ( score != -1 ) { i++; // insert code to process score // read next score score = in.readInt(); }

Reading to End of Input int i = 0; InputBox in = new InputBox(); In.setPrompt(“Enter score (empty input ends data”); // read first score score = in.readInt(); while ( !in.eoi() ) { i++; // insert code to process score // read next score score = in.readInt(); }

Loop-and-a-Half Done using while read score while ( !in.eoi( ) ) { process score read score }; Done using do-while do { read score if ( !in.eoi( ) ) process score } while ( !in.eoi( ) );

Infinite Loop with Escape Done using do-while do { read if ( in.eoi( ) ) return process } while ( true ); Done using while while (true) { read if ( in.eoi( ) ) return process score };

Drawing in Java // code to read and display GIF image from file import java.awt.*; Import CSLib.*; … DrawingBox g = new DrawingBox(); … Toolkit tools = Toolkit.getDefaultToolkit(): Image mouse = tools.getImage(“C:/KMR/images/mouse.gif”); g.drawImage(mouse, x, y);

Nested Loops int advance = 0; // Draw clock and read input repeatedly do { for (int i = 1; i <= advance; i++) { // Advance time c.setMinute(c.getMinute()+1); // Update clock display d.clear(); c.display(d, SIZE/2, SIZE/2, SIZE/2); } in.setPrompt(“Advance how many minutes?”); advance = in.readInt(); } while (!in.eoi());