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

Slides:



Advertisements
Similar presentations
Topic 17 assertions and program logic
Advertisements

Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Building Java Programs Chapter 5
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.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
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 Building Java Programs Chapter 4 Lecture 4-1: if and if/else Statements reading: 4.2 self-check: #4-5, 7, 10, 11 exercises:
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.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 4: Conditional Execution.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
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.
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 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.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 4: Control Structures II
1 Building Java Programs Chapter 5 Lecture 5-2: Random Numbers reading: 5.1, 5.6.
Loop - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 1.
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.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
1 CSE 142 Midterm Review Problems These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or modified.
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.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
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.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Copyright 2009 by Pearson Education Building Java Programs Chapter 5 Lecture 5-3: Boolean Logic reading: 5.2 self-check: # exercises: #12 videos:
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
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.
CS 112 Introduction to Programming Program Analysis; Fencepost Loops Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
Lecture 5: Program Logic
Building Java Programs
Chapter 5: Control Structures II
Midterm Review Problems
Repetition-Counter control Loop
Building Java Programs
Building Java Programs Chapter 4
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.
Topic 17 assertions and program logic
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
Indefinite loop variations
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Presentation transcript:

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

Copyright 2006 by Pearson Education 2 Lecture outline logical assertions indefinite loop variations the do/while loop the break statement

Copyright 2006 by Pearson Education 3 Logical assertions reading: 5.5

Copyright 2006 by Pearson Education 4 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 is greater than 20. x divided by 2 equals 7. (depends on the value of x)

Copyright 2006 by Pearson Education 5 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? 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)

Copyright 2006 by Pearson Education 6 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; } next == 0prev == 0next == prev Point A Point B Point C Point D Point E Which of the following assertions are true at which point(s) in the code? Choose ALWAYS, NEVER, or SOMETIMES. next == 0prev == 0next == prev Point A SOMETIMESALWAYSSOMETIMES Point B NEVERSOMETIMES Point C NEVER ALWAYS Point D SOMETIMESNEVERSOMETIMES Point E ALWAYSSOMETIMES

Copyright 2006 by Pearson Education 7 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); } x < yx == yz == 0 Point A Point B Point C Point D Point E Which of the following assertions are true at which point(s) in the code? Choose ALWAYS, NEVER, or SOMETIMES. x < yx == yz == 0 Point A SOMETIMES ALWAYS Point B NEVERSOMETIMES Point C SOMETIMES Point D SOMETIMES NEVER Point E ALWAYSNEVERSOMETIMES

Copyright 2006 by Pearson Education 8 Assertion example 3 // Assumes y >= 0, and 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; } y == 0y % 2 == 0 Point A Point B Point C Point D Point E Point F Point G Point H Which of the following assertions are true at which point(s) in the code? Choose ALWAYS, NEVER, or SOMETIMES. y == 0y % 2 == 0 Point A SOMETIMES Point B NEVERSOMETIMES Point C NEVERALWAYS Point D NEVERSOMETIMES Point E NEVER Point F SOMETIMESALWAYS Point G SOMETIMES Point H ALWAYS

Copyright 2006 by Pearson Education 9 Variations of indefinite loops (do/while and break) reading: 5.4

Copyright 2006 by Pearson Education 10 The do/while loop do/while loop: Executes statements repeatedly while a condition is true, testing it at the end of each repetition. Similar to a while loop, except that its body statement(s) will always execute the first time. do { ; } while ( ); Example: // roll until we get a # other than 3 Random rand = new Random(); int dice; do { dice = rand.nextInt(); } while (dice == 3);

Copyright 2006 by Pearson Education 11 do/while question Modify the previous Sentinel program to use a do/while. Modify the previous dice program to use a do/while loop. Example log of execution: = = = = = 7 You won after 5 tries!

Copyright 2006 by Pearson Education 12 do/while solution // Rolls two dice until a sum of 7 is reached. import java.util.*; public class Roll { public static void main(String[] args) { Random rand = new Random(); int tries = 0; int sum; do { int roll1 = rand.nextInt(6) + 1; int roll2 = rand.nextInt(6) + 1; sum = roll1 + roll2; System.out.println(roll1 + " + " + roll2 + " = " + sum); tries++; } while (sum != 7); System.out.println("You won after " + tries + " tries!"); }

Copyright 2006 by Pearson Education 13 "Forever" loop with break break statement: Immediately exits a loop. 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) { ; if ( ) { break; } ; } Exercise: Modify our Sentinel program to use break.

Copyright 2006 by Pearson Education 14 Sentinel loop with break A working sentinel loop solution using break : Scanner console = new Scanner(System.in); int sum = 0; while (true) { System.out.print("Enter a number (-1 to quit): "); int number = console.nextInt(); if (number == -1) { // don't add -1 to sum break; } sum = sum + number; // number != -1 here } System.out.println("The total was " + sum);