CSc 110, Autumn 2017 Lecture 17: while Loops and Sentinel Loops

Slides:



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

Building Java Programs
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.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Using Shortcut Arithmetic Operators Accumulator: A variable that is used to total. Its value is repeatedly increased by some amount. Java provides shortcuts.
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.
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
Building java programs, chapter 5 Program logic and indefinite loops.
Loops and Simple Functions CS303E: Elements of Computers and Programming.
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.
CS 112 Introduction to Programming Loop Patterns: break; Fencepost Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
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.
CSc 110, Autumn 2017 Lecture 13: Cumulative Sum and Boolean Logic
CSc 110, Autumn 2017 Lecture 15: Strings and Fencepost Loops
Adapted from slides by Marty Stepp and Stuart Reges
Categories of loops definite loop: Executes a known number of times.
CSc 110, Autumn 2017 Lecture 16: Fencepost Loops and Review
CSc 110, Spring 2017 Lecture 12: Random Numbers
Lecture 6: While Loops and the Math Class
Lecture 5: Program Logic
REPETITION CONTROL STRUCTURE
Lecture 7: Input and Miscellaneous
CSc 110, Spring 2017 Lecture 14: Boolean Logic and Midterm Review
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
CSc 110, Autumn 2017 Lecture 18: While loops and File Input
CSc 110, Autumn 2016 Lecture 13: Random Numbers
While Loops in Python.
Logical Operators and While Loops
CSc 110, Autumn 2016 Lecture 12: while Loops, Fencepost Loops, and Sentinel Loops Adapted from slides by Marty Stepp and Stuart Reges.
CSc 110, Spring 2017 Lecture 11: while Loops, Fencepost Loops, and Sentinel Loops Adapted from slides by Marty Stepp and Stuart Reges.
Building Java Programs Chapter 4
CSc 110, Autumn 2016 Lecture 14: Boolean Logic
Adapted from slides by Marty Stepp and Stuart Reges
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Building Java Programs
CSc 110, Spring 2017 Lecture 9: Advanced if/else; Cumulative sum
CSc 110, Spring 2018 Lecture 16: Fencepost Loops and while loops
Topic 14 while loops and loop patterns
CSc 110, Spring 2017 Lecture 13: Random numbers and Boolean Logic
CSc 110, Autumn 2016 Lecture 10: Advanced if/else; Cumulative sum
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Spring 2018 Lecture 7: input and Constants
Repetition Control Structure
Chapter 6: Repetition Statements
CSc 110, Spring 2018 Lecture 13: Cumulative Sum and Boolean Logic
Lecture 6: While Loops and the Math Class
CSc 110, Spring 2018 Lecture 5: Loop Figures and Constants
Lecture 9:The While Loop + Math methods AP Computer Science Principles
CSc 110, Spring 2018 Lecture 8: input and Parameters
Logical Operators and While Loops
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
CIS 110: Introduction to Computer Programming
Building Java Programs
Building Java Programs
Building Java Programs
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
Types of loops definite loop: A loop that executes a known number of times. Examples: Repeat these statements 10 times. Repeat these statements k times.
Building Java Programs
While Loops in Python.
Building Java Programs
Presentation transcript:

CSc 110, Autumn 2017 Lecture 17: while Loops and Sentinel Loops Adapted from slides by Marty Stepp and Stuart Reges

Categories of loops definite loop: Executes a known number of times. The for loops we have seen are definite loops. Print "hello" 10 times. Find all the prime numbers up to an integer n. Print each odd number between 5 and 127. indefinite loop: One where the number of times its body repeats is not known in advance. Prompt the user until they type a non-negative number. Print random numbers until a prime number is printed. Repeat until the user has typed "q" to quit.

The while loop while loop: Repeatedly executes its body as long as a logical test is true. while test: statement(s) Example: num = 1 # initialization while num <= 200: # test print(str(num) + " ", end='') num = num * 2 # update # output: 1 2 4 8 16 32 64 128

Example while loop # finds the first factor of 91, other than 1 n = 91 while n % factor != 0: factor += 1 print("First factor is", factor) # output: First factor is 7 while is better than for because we don't know how many times we will need to increment to find the factor.

Sentinel values sentinel: A value that signals the end of user input. sentinel loop: Repeats until a sentinel value is seen. Example: Write a program that prompts the user for text until the user types "quit", then output the total number of characters typed. (In this case, "quit" is the sentinel value.) Type a word (or "quit" to exit): hello Type a word (or "quit" to exit): yay Type a word (or "quit" to exit): quit You typed a total of 8 characters.

Solution? This solution produces the wrong output. Why? sum = 0 response = "dummy" # "dummy" value, anything but "quit" while response != "quit": response = input("Type a word (or \"quit\" to exit): ") sum += len(response) print("You typed a total of " + str(sum) + " characters.") This solution produces the wrong output. Why? You typed a total of 12 characters.

The problem with our code Our code uses a pattern like this: sum = 0 while input is not the sentinel: prompt for input; read input. add input length to the sum. On the last pass, the sentinel’s length (4) is added to the sum: prompt for input; read input ("quit"). add input length (4) to the sum. This is a fencepost problem. Must read N lines, but only sum the lengths of the first N-1.

A fencepost solution sum = 0. prompt for input; read input. # place a "post" while (input is not the sentinel): add input length to the sum. # place a "wire" Sentinel loops often utilize a fencepost "loop-and-a-half" style solution by pulling some code out of the loop.

Correct code sum = 0 # pull one prompt/read ("post") out of the loop response = input("Type a word (or \"quit\" to exit): ") while (response != "quit"): sum += len(response) # moved to top of loop print("You typed a total of " + str(sum) + " characters.")

Sentinel as a constant SENTINEL = "quit" ... sum = 0 # pull one prompt/read ("post") out of the loop response = input("Type a word (or \"" + SENTINEL + "\" to exit): ") while response != SENTINEL: sum += len(response) # moved to top of loop print("You typed a total of " + str(sum) + " characters.")