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.

Slides:



Advertisements
Similar presentations
Topic 17 assertions and program logic
Advertisements

Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
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.
Building Java Programs
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.
Loop variations do-while and for loops. Do-while loops Slight variation of while loops Instead of testing condition, then performing loop body, the loop.
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:
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
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.
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.
1 CS 177 Week 5 Recitation Slides Loops. 2 Announcements Project 2 due next Thursday at 9PM. Exam 1 this evening (switch and loop not covered) Old exams.
1 Logical Assertions. 2 Logical assertions assertion: A statement that is either true or false. Examples: –Java was created in –The sky is purple.
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.
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 5: Control Structures II
1 Building Java Programs Chapter 5 Lecture 5-2: Random Numbers reading: 5.1, 5.6.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
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 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:
Categories of loops definite loop: Executes a known number of times.
Building Java Programs
Chapter 5: Control Structures II
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
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
Indefinite loop variations
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Presentation transcript:

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 correct section unless you fill out web-site form in advance Sample exam(s) posted Friday Another sample in section next week And problems on Practice-It! Exam will have 2-3 programming problems harder, after other problems See sample exam(s) Review session next week Time/place to-be-determined probably Thursday late afternoon

Copyright 2008 by Pearson Education 2 Building Java Programs Chapter 5 Lecture 5-3: Assertions, do/while loops reading: self-check: 22-24, 26-28

Copyright 2008 by Pearson Education 3 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) An assertion might be false ("The sky is purple" above), but it is still an assertion because it is a true/false statement.

Copyright 2008 by Pearson Education 4 Reasoning about assertions Suppose you have the following code: if (x > 3) { // Point A x--; } else { // Point B x++; } // Point C What do you know about x 's value at the three points? Is x > 3 ? Always? Sometimes? Never?

Copyright 2008 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? (SOMETIMES) (ALWAYS) (SOMETIMES) (NEVER)

Copyright 2008 by Pearson Education 6 Reasoning about assertions Right after a variable is initialized, its value is known: int x = 3; // is x > 0? ALWAYS In general you know nothing about parameters' values: public static void mystery(int a, int b) { // is a == 10? SOMETIMES But inside an if, while, etc., you may know something: public static void mystery(int a, int b) { if (a < 0) { // is a == 10? NEVER... } }

Copyright 2008 by Pearson Education 7 Assertions and loops At the start of a loop's body, the loop's test must be true : while (y < 10) { // is y < 10? ALWAYS... } Immediately after a loop, the loop's test must be false : while (y < 10) {... } // is y < 10? NEVER Inside a loop's body, the loop's test may become false : while (y < 10) { y++; // is y < 10? SOMETIMES }

Copyright 2008 by Pearson Education 8 More on loops Remember that a loop might execute 0 or more times public static void m(int y) { int x = 0; while (y < 10) { ++x;... // no other changes to x } // is x > 0? SOMETIMES }

Copyright 2008 by Pearson Education 9 "Sometimes" Things that cause a variable's value to be unknown (often leads to "sometimes" answers): reading from a Scanner reading a number from a Random object a parameter's initial value to a method If you can reach a part of the program both with the answer being "yes" and the answer being "no", then the correct answer is "sometimes". If you're unsure, "Sometimes" is a good guess.

Copyright 2008 by Pearson Education 10 Perspective Assertions are a great way to think about your program And what all our language constructs are actually good at Purpose of assignments: change whether assertions hold Purpose of tests: learn more about what assertions hold Purpose of ifs/loops: have different code points for different possibilities “If I get here, then x must be less than y, so it’s okay to…” (Plus, it’s on the midterm)

Copyright 2008 by Pearson Education 11 Assertion example 0 public static void mystery(int x) { int y = 10; // Point A while (x < y) { // Point B x++; // Point C } // Point D System.out.println(y); } x < yx == y Point A Point B Point C Point D SOMETIMES ALWAYSNEVER SOMETIMES NEVERSOMETIMES Which of the following assertions are true at which point(s) in the code? Choose ALWAYS, NEVER, or SOMETIMES.

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

Copyright 2008 by Pearson Education 13 Assertion example 2 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 SOMETIMESALWAYSSOMETIMES NEVERSOMETIMES NEVER ALWAYS SOMETIMESNEVERSOMETIMES ALWAYSSOMETIMES Which of the following assertions are true at which point(s) in the code? Choose ALWAYS, NEVER, or SOMETIMES.

Copyright 2008 by Pearson Education 14 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 * x; y = y / 2; // Point D } else { // Point E prod = prod * x; y--; // Point F } // Point G return prod; } y > 0y % 2 == 0 Point A Point B Point C Point D Point E Point F Point G 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 ALWAYSSOMETIMES Point C ALWAYS Point D ALWAYSSOMETIMES Point E ALWAYSNEVER Point F SOMETIMESALWAYS Point G NEVERALWAYS

Copyright 2008 by Pearson Education 15 Another non-useless example /* This method program prompts the user for numbers until -1 is typed, then returns the largest number typed (or -1 if that was the only number typed). */ public static int biggest(Scanner console) { System.out.print("Type a number (or -1 to quit): "); int number = console.nextInt(); int max = number; // max >= number: ALWAYS while (number != -1) { // max >= number: SOMETIMES if (number > max) { max = number; } // max >= number: ALWAYS System.out.print("Type a number (or -1 to quit): "); number = console.nextInt(); } // max >= number: SOMETIMES (!) return max; }

Copyright 2008 by Pearson Education 16 Another non-useless example /* Prompts the user for numbers until -1 is typed. Returns the largest positive number typed. Requires at least one positive.*/ public static int biggest(Scanner console) { int number = -1; int max = -1; while(number <= 0) { System.out.print("Type a positive number: "); number = console.nextInt(); } max = number; // max >= number: ALWAYS, max > 0: ALWAYS while (number != -1) { // max >= number: SOMETIMES, max > 0: ALWAYS if (number > max) { max = number; } // max >= number: ALWAYS, max > 0: ALWAYS System.out.print("Type a number (or -1 to quit): "); number = console.nextInt(); } // max >= number: ALWAYS, max > 0: ALWAYS return max; }

Copyright 2008 by Pearson Education 17 while loop variations reading: 5.4 self-checks: #22-24 exercises: #6

Copyright 2008 by Pearson Education 18 The do/while loop do/while loop: Executes statements repeatedly while a condition is true, testing it at the end of each repetition. do { statement(s) ; } while ( test ); Example: // prompt until the user gets the right password String phrase; do { System.out.print("Password: "); phrase = console.next(); } while (!phrase.equals("abracadabra"));

Copyright 2008 by Pearson Education 19 do/while flow chart How does this differ from the while loop? The controlled statement(s) will always execute the first time, regardless of whether the test is true or false.

Copyright 2008 by Pearson Education 20 Thoughts on do/while Not used very often; optional in 142 Affects assertions: body always executes at least once body executes once before test public static void m(int y) { int x = 0; do { // is y < 10? SOMETIMES ++x;... // no other changes to x } while(y < 10); // is x > 0? ALWAYS }

Copyright 2008 by Pearson Education 21 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. while (true) { statement(s) ; if ( test ) { break; } statement(s) ; } break is often bad style! Do not use it on CSE 142 homework!

Copyright 2008 by Pearson Education 22 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);

Copyright 2008 by Pearson Education 23 Thoughts on break Literal meaning is go to after the loop right now Affects assertions: No longer know the loop test is false right after the loop public static void m(int y, int x) { while(y < 10) { if(y==x) { break; } ++y; } // is y >= 10? SOMETIMES } Can also use return anywhere in a method Returns right now