1 CS1110 23 October 2008 The while loop and assertions Read chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Quotes for the Day:

Slides:



Advertisements
Similar presentations
50.530: Software Engineering Sun Jun SUTD. Week 10: Invariant Generation.
Advertisements

1 Chapter Six Algorithms. 2 Algorithms An algorithm is an abstract strategy for solving a problem and is often expressed in English A function is the.
1 CS March 2009 While loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim in two days: Th 7:30-9pm Uris Aud.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
CS100J October 07, 2003 Loops Repetitive statements, or iterative statements, or loops “O! Thou hast damnable iteration and art, indeed, able to corrupt.
1 CS100J October 06, 2005 For Loops Reading: Secs Quote for the Day: Perhaps the most valuable result of all education is the ability to make yourself.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
1 CS100J 15 March, 2006 The while loop and assertions Read chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal.
CS 1110 Prelim III: Review Session 1. Info My name: Bruno Abrahao – We have two other TA’s in the room to help you individually Beibei Zhu Suyong Zhao.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 Recitation 7. Developing loops Introduction. This recitation concerns developing loops using their invariants and bound functions. Your recitation instructor.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved For Loops October 16, 2013 Slides by Evan Gallagher.
Introduction to Programming (in C++) Algorithms on sequences. Reasoning about loops: Invariants. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept.
1 CS100J 09 March, 2006 More on Loops Reading: Secs 7.1–7.4 A Billion. The next time you hear someone in government rather casually use a number that includes.
Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
1 CS1110, 8 March 2009 Two topics: elementary graphics (for A5); loops Reading: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can.
1 CS March 2010 Read: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal pointI'm.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
CSE1222: Lecture 7The Ohio State University1. logExample.cpp // example of log(k) for k = 1,2,..,8... int main() { cout
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Mar 3, 2006.
Chapter 5: Sequences, Mathematical Induction, and Recursion 5.5 Application: Correctness of Algorithms 1 [P]rogramming reliability – must be an activity.
1 CS100J 4 March 2008 Two topics: Turtles; loops Start reading Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Feb 18, 2005.
1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.
Java Basics Hussein Suleman March 2007 UCT Department of Computer Science Computer Science 1015F.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
1 CS April 2010 while loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim 2. Thursday evening, 7:30PM Watch.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
1 CS April 2010 while loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim 2. Thursday evening, 7:30PM Watch.
1 CS100J September 27, 2003 Loops Repetitive statements, or iterative statements, or loops “O! Thou hast damnable iteration and art, indeed, able to corrupt.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
1 CS1110 Lecture 16, 26 Oct 2010 While-loops Reading for next time: Ch (arrays) Prelim 2: Tu Nov 9 th, 7:30-9pm. Last name A-Lewis: Olin 155 Last.
1 CS100J 06 March 2008 Read: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal pointI'm.
CS100A, Fall Review of loops 1 CS100A, Fall 1998, Review of Loops and Loop Invariants Some students are still having trouble understanding loop invariants.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2015.
Repetition Statements
Chapter 4 Repetition Statements (loops)
Correctness issues and Loop invariants
Start reading Sec and chapter 7 on loops
2.5 Another Java Application: Adding Integers
CS October 2009 Read: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal point I'm.
CS October 2008 The while loop and assertions
Outline Altering flow of control Boolean expressions
Testing change to a linked list
Introduction to Object-Oriented Programming with Java--Wu
Control Structure Chapter 3.
Binary Search and Loop invariants
Asymptotic complexity
Unit 3: Variables in Java
Just Enough Java 17-May-19.
CS100A Lecture 25 1 December 1998 Chance to replace grade on Prelim 3, Question 2. Everyone is expected to be in lecture on Thursday, 3 December.
(We’ll spend a few lectures on iteration)
CS100J 16 Oct, 2007 Assignment A5: loops
CS October 2010 Read: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal point I'm.
Presentation transcript:

1 CS October 2008 The while loop and assertions Read chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Quotes for the Day: Instead of trying out computer programs on test cases until they are debugged, one should prove that they have the desired properties. John McCarthy, 1961, A basis for a mathematical theory of computation. Testing may show the presence of errors, but never their absence. Dijkstra, Second NATO Conf. on Software Engineering, A week of hard work on a program can save you 1/2 hour of thinking. Paul Gries, CS, University of Toronto, 2005.

2 The while loop: syntax while ( ) while ( { sequence of declarations and statements } : a boolean expression. : a statement. BUT: We almost always make the a block. condition repetend false true

3 The while loop System.out.println(5*5); System.out.println(6*6); System.out.println(7*7); System.out.println(8*8); int k= 5; while ( k <= 8) { System.out.println(k*k); k= k+1; } To execute the while loop: (1)Evaluate condition k <= 8; if false, stop execution. (2) Execute the repetend. (3) Repeat again from step (1). Trace execution of the loop: Study section shows you how to “trace” execution of a loop. k= 5; k <= 8 System.out.println(k*k); k= k+1; false true

4 For loop, corresponding while loop ; for (int k= b; k <= c; k= k+1) { Process k } ; int k= b; while (k <= c) { Process k; k= k+1; }

5 // Set c to the number of ‘e’s in String s. int n= s.length(); c= 0; // invariant: c = number of ‘e’s in s[0..k-1] for (int k= 0; k < n; k= k+1) { if (s.charAt(k) == ‘e’) c= c + 1; } // c = number of ‘e’s in s[0..n-1] 4 loopy questions 1. How does it start? (what is the initialization?) 2. When does it stop? (From the invariant and the falsity of loop condition, deduce that result holds.) 3. How does it make progress toward termination? 4. How does repetend keep invariant true?

6 // Set c to the number of ‘e’s in String s. int n= s.length(); c= 0; k= 0; // invariant: c = number of ‘e’s in s[0..k-1] while (k < n) { if (s.charAt(k) == ‘e’) c= c + 1; k= k+ 1; } // c = number of ‘e’s in s[0..n-1] The while loop: 4 loopy questions. Allows us to focus on one thing at a time. Separate our concerns. 1. How does it start? (what is the initialization?) 2. When does it stop? (From the invariant and the falsity of loop condition, deduce that result holds.) 3. How does it make progress toward termination? 4. How does repetend keep invariant true?

7 Understanding assertions about lists This is an assertion about v and k. It is true because chars of v[0..3] are greater than ‘C’ and chars of v[6..8] are ‘Z’s X Y Z X A C Z Z Z v This is a list of Characters v ≥ C ? all Z’s k k 8 v ≥ C ? all Z’s k k 8 v ≥ C all Z’s k 6 0 k 8 v ≥ W A C all Z’s k 4 0 k 8 Indicate whether each of these 3 assertions is true or false.

8 We add the postcondition and also show where the invariant must be true: initialization; // invariant: P while ( B ) { // { P and B} repetend // { P } } // { P } // { Result R } The four loopy questions Suppose we have this while loop, with initialization: initialization; while ( B ) { repetend } Second box helps us develop four loopy questions for developing or understanding a loop: 1. How does loop start? Initialization must truthify inv P. 2. When does loop stop? At end, P and !B are true, and these must imply R. Find !B that satisfies P && !B => R. 3. Make progress toward termination? Put something in repetend to ensure this. 4. How to keep invariant true? Put something in repetend to ensure this.

9 Develop loop to store in x the sum of How should the loop start? Make range 1..k–1 empty: k= 1; x= 0; We’ll keep this definition of x and k true: x = sum of 1..k–1 2. When can loop stop? What condition lets us know that x has result? When k == How can repetend make progress toward termination? k= k+1; 4. How do we keep def of x, h, k true? x= x + k; Four loopy questions k= 1; x= 0; // invariant: x = sum of 1..(k–1) while ( k != 101) { x= x + k; k= k + 1; } // { x = sum of }

10 Roach infestation! /** = number of weeks it takes roaches to fill the apartment --see p 244 of text*/ public static int roaches() { double roachVol=.001; // Space one roach takes double aptVol= 20*20*8; // Apartment volume double growthRate= 1.25; // Population growth rate per week int w= 0; // number of weeks int pop= 100; // roach population after w weeks // inv: pop = roach population after w weeks AND // before week w, volume of the roaches < aptVol while (aptVol > pop * roachVol ) { pop= (int) (pop * growthRate); w= w + 1; } return w; }

11 Iterative version of logarithmic algorithm to calculate b**c. /** set z to b**c, given c ≥ 0 */ int x= b; int y= c; int z= 1; // invariant: z * x**y = b**c and 0 ≤ y ≤ c while (y != 0) { if (y % 2 == 0) { x= x * x; y= y/2; } else { z= z * x; y= y – 1; } } // { z = b**c } Rest on identities: b**0 = 1 b**c = b * b**(c-1) for even c, b**c = (b*b)**(c/2) 3*3 * 3*3 * 3*3 * 3*3 = 3**8 (3*3)*(3*3)*(3*3)*(3*3) = 9**4 Algorithm is logarithmic in c, since time is proportional to log c /** = b**c, given c ≥ 0 */ public static int exp(int b, int c) { if (c == 0) return 1; if (c%2 = 0) return exp(b*b, c/2); return b * exp(b, c–1); }

12 Calculate quotient and remainder when dividing x by y x/y = q + r/y 21/4= 4 + 3/4 Property: x = q * y + r and 0 ≤ r < y /** Set q to and r to remainder. Note: x >= 0 and y > 0 */ int q= 0; int r= x; // invariant: x = q * y + r and 0 ≤ r while (r >= y) { r= r – y; q= q + 1; } // { x = q * y + r and 0 ≤ r < y }