Chapter 6 Repetition Asserting Java © Rick Mercer.

Slides:



Advertisements
Similar presentations
Selection Control Structures Chapter 5: Selection Asserting Java © Rick Mercer.
Advertisements

June 10, 2015ICS102: while & do-while1 while and do-while Statements.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Chapter 6 Repetition Asserting Java © Rick Mercer.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Chapter 5: Loops and Files.
09 Non-deterministic iteration1June Non-deterministic iteration CE : Fundamental Programming Techniques.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
© 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.
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
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
Java Programming: From the Ground Up
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.
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 6A While Loops Asserting Java © Rick Mercer.
Chapter 4: Control Structures II
Chapter 5: Control Structures II
Control Structures II: Repetition.  Learn about repetition (looping) control structures  Explore how to construct and use count-controlled, sentinel-controlled,
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.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
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.
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.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Looping Increment/Decrement Switch. Flow of Control Iteration/Switch Statements.
Chapter 6 Repetition Asserting Java © Rick Mercer.
Java Fundamentals 4. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
while Repetition Structure
Chapter 5: Control Structures II
Chapter 6, Part 1 While Loops Asserting Java © Rick Mercer 1.
Determinate Loop Pattern, Java's for Statement, and Scanner objects
Chapter 5: Control Structures II
Java's for Statement.
Repetition-Counter control Loop
Repetition-Sentinel,Flag Loop/Do_While
Chapter 5: Control Structures II
TK1114 Computer Programming
Determinate Loop Pattern, Java's for Statement, and Scanner objects
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Determinate Loops with the
Outline Altering flow of control Boolean expressions
Chapter 8 Repetition Computing Fundamentals with C++ 3rd Edition
Control Statements Loops.
Chapter 6 Repetition Asserting Java © Rick Mercer 1.
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 14, 2011
Control Statements Loops.
Repetition Statements
Building Java Programs
Announcements Lab 3 was due today Assignment 2 due next Wednesday
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Chapter 6, Part 1 While Loops Asserting Java © Rick Mercer 1.
Computer Science Club 1st November 2019.
Presentation transcript:

Chapter 6 Repetition Asserting Java © Rick Mercer

Algorithmic Pattern: The Determinate loop  We often need to perform some action a specific number of times: — Produce 89 paychecks. — Count down to 0 (take 1 second of the clock). — Compute grades for 81 students  The determinate loop pattern repeats some action a specific number of times.

While Loop flowchart Boolean Expression true { statement_1; statement_2; statement_n; } false

Determinate Loops with while Determinate Loops with while  The determinate loop pattern can be implemented with the Java while loop  This template repeats a process n times: int n = /* how often we must repeat the process */ int n = /* how often we must repeat the process */ int counter = 1; while ( counter <= n ) { int counter = 1; while ( counter <= n ) { // the process to be repeated // the process to be repeated counter = counter + 1; } counter = counter + 1; } — determinate loops must know the number of repetitions before they begin: know exactly how many employees, or students, or whatever that must be processed, for example

Example while loop produces an average Scanner keyboard = new Scanner(System.in); double sum = 0.0; double number; System.out.print("How many do you want to average? "); int n = keyboard.nextInt(); int counter = 1; // Do something n times while (counter <= n) { System.out.print("Enter number: "); // <- Repeat 3 number = keyboard.nextDouble(); // <- statements sum = sum + number; // <- n times counter = counter + 1; // make sure the loop stops } double average = sum / n; System.out.print("Average of "+ n + " numbers is "+ average);

Active Learning  What is the output? int counter = 1; int n = 5; while(counter <= n) { System.out.print(counter + " "); counter = counter + 1; } Output: ____________________________ int loopControlVariable = 0; while(loopControlVariable <= 2 * n) { System.out.print(loopControlVariable + " "); loopControlVariable = loopControlVariable + 2; } Output: ____________________________

Indeterminate Loops  Determinate loops have a limitation — We must know n (the number of repetitions) in advance  Many situations need us to repeat a set of statements an unspecified number of times: — Processing report cards for every student in a school (or paychecks for all employees, or...) — Allowing 1 to many ATM transactions — Asking the user for specific input and allowing re-entry of input after invalid inputs

Some things that terminate indeterminate loops  An indeterminate loop repeats a process until some stopping event terminates the repetition  There are many such events, but we'll focus on these: — User enters a special value indicating end of data — A logical expression becomes false — The Grid's mover hits the wall or an edge — The end of a file is encountered  Indeterminate loops do not need to know n in advance  Indeterminate loops can actually determine n

Pattern Indeterminate loop Problem Some process must repeat an unknown number of times so some event is needed to terminate the loop. Algorithm while( the termination event has not occurred ) { execute several actions1 bring the loop closer to termination } Code while(dog.frontIsClear()) { dog.move(); Example }

While loop with a Scanner  Sometimes a stream of input from the keyboard or a file needs to be read until there is no more data in the input stream  Consider a Scanner object constructed with a String argument — The string represents an input stream  You will need Scanner in ControlFun

These assertions public void showScanner() { Scanner scannerWithInts = new Scanner("1 2 3"); assertEquals(1, scannerWithInts.nextInt()); assertEquals(2, scannerWithInts.nextInt()); assertEquals(3, scannerWithInts.nextInt()); Scanner scanner = new Scanner("There are five words here."); assertEquals("There", scanner.next()); assertEquals("are", scanner.next()); assertEquals("five", scanner.next()); assertEquals("words", scanner.next()); assertEquals("here.", scanner.next()); }

Write method average to return the average of the doubles in a public void testNum100s() { LoopFun cf = new LoopFun(); Scanner scanner1 = new Scanner(" "); Scanner scanner2 = new Scanner("1.0\t 2\n "); assertEquals(80.0, cf.average(scanner). 0.01); assertEquals(2.5, cf.average(scanner). 0.01); }

@Test public void testSumOfNegs() { ControlFun cf = new ControlFun(); Scanner scanner0 = new Scanner("1 2 3"); Scanner scannerA = new Scanner(" "); Scanner scannerB = new Scanner(" "); assertEquals(0, cf.sumOfNegatives(scanner0)); assertEquals(-6, cf.sumOfNegatives(scannerA)); assertEquals(-10, cf.sumOfNegatives(scannerB)); } Let sumOfNegatives return the sum of the negative integers in a Scanner

What's wrong with this method? public int sumOfNegatives(Scanner scanner) { int result = 0; while (scanner.hasNextInt()) { if (scanner.nextInt() < 0) { result += scanner.nextInt(); } return result; }