CIS 110: Introduction to Computer Programming

Slides:



Advertisements
Similar presentations
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Advertisements

Building Java Programs
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
LAB 10.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
Chapter 5 Loops.
1 Fencepost loops “How do you build a fence?”. 2 The fencepost problem Problem: Write a class named PrintNumbers that reads in an integer called max and.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops; Procedural Design reading: 5.1 – 5.2; 4.5.
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
Topic 14 while loops and loop patterns Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
1 Building Java Programs Chapter 5 Lecture 5-2: Random Numbers reading: 5.1, 5.6.
Building java programs, chapter 5 Program logic and indefinite loops.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 12/11/20151.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
Building Java Programs Program Logic and Indefinite Loops.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 2/4/20161.
Copyright 2010 by Pearson Education Building Java Programs Scanner ; if / else; while loops ; random reading: 3.3 – 3.4, 4.1, 4.5, 5.1, 5.6.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 5.1 – 5.2.
Lecture 4 CS140 Dick Steflik. Reading Keyboard Input Import java.util.Scanner – A simple text scanner which can parse primitive types and strings using.
Categories of loops definite loop: Executes a known number of times.
CSc 110, Autumn 2017 Lecture 17: while Loops and Sentinel Loops
Lecture 5: Program Logic
Building Java Programs
Building Java Programs
Building Java Programs Chapter 5
Building Java Programs
Chapter 5: Control Structures II
Repetition-Counter control Loop
Repetition-Sentinel,Flag Loop/Do_While
Selected Topics From Chapter 6 Iteration
TK1114 Computer Programming
CSc 110, Autumn 2016 Lecture 12: while Loops, Fencepost Loops, and Sentinel Loops Adapted from slides by Marty Stepp and Stuart Reges.
Building Java Programs Chapter 4
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
CSc 110, Spring 2018 Lecture 16: Fencepost Loops and while loops
Topic 14 while loops and loop patterns
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Loop problem Write a method printLetters that prints each letter from a word separated by commas. For example, the call: printLetters("Atmosphere") should.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Repetition Statements
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Outline Boolean Expressions The if Statement Comparing Data
Building Java Programs
CSE 142, Spring 2013 Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 5.1 – 5.2.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
CIS 110: Introduction to Computer Programming
Presentation transcript:

CIS 110: Introduction to Computer Programming Lecture 13 Indefinite Loops (§ 5.1-5.2) 4/6/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Outline Indefinite loops with while Fencepost and sentinel loops 4/6/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Indefinite Loops 4/6/2019 CIS 110 (11fa) - University of Pennsylvania

Indefinite Loop Bounds So far we’ve known the bounds of our loops before we’ve executed the loop themselves. e.g., for (int i = 0; i < 10; i++) { /* ... */ } Many loops don't offer that luxury... // while the user hasn't input "yes" yet // Ask the user for input 4/6/2019 CIS 110 (11fa) - University of Pennsylvania

Problem: firstDivisor Problem: write a method firstDivisor(x, y) that returns the first number that divides x starting at y and going up. Example: firstDivisor(26, 10) = 13 Indefinite behavior: the amount of numbers we'll check depends on x and y. 4/6/2019 CIS 110 (11fa) - University of Pennsylvania

firstDivisor Solution public static int firstDivisor(int x, int y) { while (x % y != 0) { y += 1; } return y; public static void main(String[] args) { // Output: 13 System.out.println(firstDivisor(26, 10)); 4/6/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania While loops "While the guard is true, execute the body". Like an if-statement, but looping! Guard while (x % y != 0) { y += 1; } Body 4/6/2019 CIS 110 (11fa) - University of Pennsylvania

While Loops vs. For Loops for (int i = 0; i < 10; i++) { System.out.println(i); } int i = 0; while (i < 10) { System.out.println(i); i++; } Can express the same kinds of loops. Some benefit to for over while (i.e., scoping). for is meant for definite loops: "loop x times". while is meant for indefinite loops: "loop until some condition is met". 4/6/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania The Random Object Random rand = new Random(); double value = 0; while(value <= 0.5) { System.out.printf("%.2f is less than or equal to 5.\n", value); // nextDouble returns a double between 0.0 and 1.0 value = rand.nextDouble(); } System.out.printf("%.2f is greater than 5!\n", value); Random objects to generate (pseudo)-random numbers "Pseudo"-random because they are still the result of mathematical formula 4/6/2019 CIS 110 (11fa) - University of Pennsylvania

Method calls of the Random object Random rand = new Random(); // Prints a random integer betweem -2^31 to (2^31)-1 System.out.println(rand.nextInt()); // Prints a random integer between 0 and 9 System.out.println(rand.nextInt(10)); // Prints out a random double starting at 0.0 up to // (but not including) 1.0 System.out.println(rand.nextDouble()); // Prints either true or false randomly System.out.println(rand.nextBoolean()); 4/6/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Simulations and Games Application of indefinite loops. Repeatedly executes until some condition is met. E.g., simulating a random walk. Random rand = new Random(); int position = 1; while (position > 0) { System.out.println("I am currently at " + position); if (rand.nextBoolean()) { position += 1; } else { position -= 1; } System.out.println("I am back home!"); 4/6/2019 CIS 110 (11fa) - University of Pennsylvania

Fencepost and Sentinel Loops 4/6/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania The fencepost problem Problem: write a method fencepost(n) that takes an integer and draws a fencepost of length n. e.g., fencepost(5) prints |=|=|=|=| 4/6/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Fencepost solution? public static void fencepost(int n) { for (int i = 0; i < n; i++) { System.out.print("|="); } System.out.println(); Not good enough! Prints out an extra wire, e.g., |=|=|=|=|= 4/6/2019 CIS 110 (11fa) - University of Pennsylvania

Hoisting is the solution! public static void fencepost(int n) { System.out.print("|"); for (int i = 1; i < n; i++) { System.out.print("=|"); } System.out.println(); We hoisted part of the first iteration of the loop (i.e., the first post) and flipped the body. Now the pattern works! Loop-and-a-half is a common pattern! 4/6/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Sentinels Sentinels are values that designate when a loop should end. Problem: write a loop that sums up positive integers from the user until they enter -1 to end the process. -1 is the sentinel value in this loop. // while the user's input isn't -1 // get an input from the user and add it to our running sum. 4/6/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Sentinel solution? Scanner in = new Scanner(System.in); int sum = 0; int input = 0; // Prime loop so we enter it initially. while (input != -1) { System.out.print("num? "); input = in.nextInt(); sum += input; } System.out.println("sum = " + sum); Not good enough! Prints out one less than the sum? Why? 4/6/2019 CIS 110 (11fa) - University of Pennsylvania

Solution: hoist out some input! Scanner in = new Scanner(System.in); int sum = 0; // Hoist out half of the loop! System.out.print("num? "); int input = in.nextInt(); while (input != -1) { sum += input; input = in.nextInt(); } System.out.println("sum = " + sum); Now it works! We hoisted out one prompt out of the loop and changed the order of summation and prompting. 4/6/2019 CIS 110 (11fa) - University of Pennsylvania