Indefinite loop variations

Slides:



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

Copyright 2010 by Pearson Education Building Java Programs Chapter 4 Lecture 4-2: Advanced if/else ; Cumulative sum reading: 4.1, 4.3, 4.5; "Procedural.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
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.
Copyright 2008 by Pearson Education 1 Midterm announcements Next week on Friday May 8 Must bring an ID Open book, open notes, closed electronics Must attend.
CS305j Introduction to Computing While Loops 1 Topic 15 Indefinite Loops - While Loops "If you cannot grok [understand] the overall structure of a program.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Building Java Programs Chapter 5
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
CS305j Introduction to Computing Odds and Ends 1 Topic 16 Creating Correct Programs "It is a profoundly erroneous truism, repeated by all the copybooks,
1 Logical Assertions. 2 Logical assertions assertion: A statement that is either true or false. Examples: –Java was created in –The sky is purple.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
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.
CS 112 Introduction to Programming Program Analysis Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
1 Building Java Programs Chapter 5 Lecture 5-2: Random Numbers reading: 5.1, 5.6.
1 Reasoning about assertions Readings: Assertions assertion: A statement that is either true or false. Examples:  Java was created in (true)
Building java programs, chapter 5 Program logic and indefinite loops.
1 CSE 142 Lecture Notes Conditional Execution with if Statements; Methods that Return Values (briefly) Chapters 3 and 4 Suggested reading: ;
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.
Copyright 2009 by Pearson Education Building Java Programs Chapter 5 Lecture 5-3: Boolean Logic reading: 5.2 self-check: # exercises: #12 videos:
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
CS 112 Introduction to Programming Program Analysis; Fencepost Loops Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
Building Java Programs
Categories of loops definite loop: Executes a known number of times.
Building Java Programs Chapter 5
Building Java Programs
Chapter 5: Control Structures II
Loop Structures.
Building Java Programs
Topic 11 Scanner object, conditional execution
Building Java Programs
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Logical assertions assertion: A statement that is either true or false. Examples: Java was created in The sky is purple. 23 is a prime number. 10.
Building Java Programs
Topic 17 assertions and program logic
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Presentation transcript:

Indefinite loop variations suggested reading: 5.4

The do/while loop Java has another kind of loop named the do/while loop. It is almost identical to the while loop, except that its body statement(s) will always execute the first time, regardless of whether the condition is true. The do/while loop, general syntax: do { <statement(s)> ; } while (<condition>); Example: // roll until we get a number other than 3 Random rand = new Random(); int dice; dice = rand.nextInt(); } while (dice == 3);

do/while loop flow chart

"Forever" loop with break break statement: Immediately exits a loop when reached. Can be used to write a loop whose test is in the middle. Such loops are often called "forever" loops because their header's boolean test is often changed to a trivial true. "forever" loop, general syntax: while (true) { <statement(s)> ; if (<condition>) { break; }

More correct code This code correctly implements a sentinel loop: int sum = 0; while (true) { System.out.print("Enter a number (-1 to quit): "); int inputNumber = console.nextInt(); if (inputNumber == -1) { break; } sum += inputNumber; // inputNumber != -1 here System.out.println("The total was " + sum);

Random/while question Write a multiplication tutor program. Example log of execution: This program helps you practice multiplication by asking you random multiplication questions with numbers ranging from 1 to 20 and counting how many you solve correctly. 14 * 8 = 112 Correct! 5 * 12 = 60 8 * 3 = 24 5 * 5 = 25 20 * 14 = 280 19 * 14 = 256 Incorrect; the correct answer was 266 You solved 5 correctly. Use a class constant for the maximum value of 20.

User errors suggested reading: 5.3

Testing for valid user input A Scanner object has methods that can be used to "look ahead" to test whether the upcoming input token is of a given type: Each of these methods waits for the user to type input tokens and press Enter, then reports a true or false answer. The hasNext and hasNextLine methods are not useful until we learn how to read input from files in Chapter 6. Method Description hasNext() Whether or not the next token can be read as a String (always true for console input) hasNextInt() Whether or not the next token can be read as an int hasNextDouble() Whether or not the next token can be read as a double hasNextLine() Whether or not the next line of input can be read as a String (always true for console input)

Scanner condition example The Scanner's hasNext methods are useful for testing whether the user typed the right kind of token for our program to use, before we read it (and potentially cause an exception!). Scanner console = new Scanner(System.in); System.out.print("How old are you? "); if (console.hasNextInt()) { int age = console.nextInt(); // will not throw an exception System.out.println("Retire in " + (65 - age) + " years."); } else if (console.hasNextDouble()) { System.out.println("Please use a whole number for your age!"); } else { System.out.println("You did not type a number."); }

Assertions suggested reading: 5.5

Logical assertions assertion: A declarative statement that is either true or false. Examples: Java was created in 1995. The sky is purple. 7 is a prime number. 10 > 20. x % 2 == 0. (depends on the value of x)

Assertions in code We can make assertions about our code and ask whether they are true at various points in the code. Valid answers are ALWAYS, NEVER, or SOMETIMES. System.out.print("Type a nonnegative number: "); double number = console.nextDouble(); // Point A: is number < 0.0 here? while (number < 0.0) { // Point B: is number < 0.0 here? System.out.print("Negative; try again: "); number = console.nextDouble(); // Point C: is number < 0.0 here? } // Point D: is number < 0.0 here?

Assertions in code answers We can make assertions about our code and ask whether they are true at various points in the code. Valid answers are ALWAYS, NEVER, or SOMETIMES. System.out.print("Type a nonnegative number: "); double number = console.nextDouble(); // Point A: is number < 0.0 here? (SOMETIMES) while (number < 0.0) { // Point B: is number < 0.0 here? (ALWAYS) System.out.print("Negative; try again: "); number = console.nextDouble(); // Point C: is number < 0.0 here? (SOMETIMES) } // Point D: is number < 0.0 here? (NEVER)

Assertion example 1 public static int mystery(Scanner console) { int prev = 0; int count = 0; int next = console.nextInt(); // Point A while (next != 0) { // Point B if (next == prev) { // Point C count++; } prev = next; next = console.nextInt(); // Point D // Point E return count; Which of the following assertions are true at which point(s) in the code? Choose ALWAYS, NEVER, or SOMETIMES. next == 0 prev == 0 next == prev Point A Point B Point C Point D Point E

Assertion example 1 answer public static int mystery(Scanner console) { int prev = 0; int count = 0; int next = console.nextInt(); // Point A while (next != 0) { // Point B if (next == prev) { // Point C count++; } prev = next; next = console.nextInt(); // Point D // Point E return count; Which of the following assertions are true at which point(s) in the code? Choose ALWAYS, NEVER, or SOMETIMES. next == 0 prev == 0 next == prev Point A SOMETIMES ALWAYS Point B NEVER Point C Point D Point E

Assertion example 2 public static void mystery(int x, int y) { int z = 0; // Point A while (x >= y) { // Point B x -= y; // Point C z++; // Point D } // Point E System.out.println(z + " " + x); Which of the following assertions are true at which point(s) in the code? Choose ALWAYS, NEVER, or SOMETIMES. x < y x == y z == 0 Point A Point B Point C Point D Point E

Assertion example 2 answer public static void mystery(int x, int y) { int z = 0; // Point A while (x >= y) { // Point B x -= y; // Point C z++; // Point D } // Point E System.out.println(z + " " + x); Which of the following assertions are true at which point(s) in the code? Choose ALWAYS, NEVER, or SOMETIMES. x < y x == y z == 0 Point A SOMETIMES ALWAYS Point B NEVER Point C Point D Point E

Assertion example 3 Which of the following assertions are true // pre : y >= 0, post: returns x^y public static int pow(int x, int y) { int prod = 1; // Point A while (y > 0) { // Point B if (y % 2 == 0) { // Point C x *= x; y /= 2; // Point D } else { // Point E prod *= x; y--; // Point F } // Point G // Point H return prod; Which of the following assertions are true at which point(s) in the code? Choose ALWAYS, NEVER, or SOMETIMES. y == 0 y % 2 == 0 Point A Point B Point C Point D Point E Point F Point G Point H

Assertion example 3 answer // pre : y >= 0, post: returns x^y public static int pow(int x, int y) { int prod = 1; // Point A while (y > 0) { // Point B if (y % 2 == 0) { // Point C x *= x; y /= 2; // Point D } else { // Point E prod *= x; y--; // Point F } // Point G // Point H return prod; Which of the following assertions are true at which point(s) in the code? Choose ALWAYS, NEVER, or SOMETIMES. y == 0 y % 2 == 0 Point A SOMETIMES Point B NEVER Point C ALWAYS Point D Point E Point F Point G Point H