1 CS100J October 06, 2005 For Loops Reading: Secs 7.5-7.6 Quote for the Day: Perhaps the most valuable result of all education is the ability to make yourself.

Slides:



Advertisements
Similar presentations
Reasoning About Code; Hoare Logic, continued
Advertisements

1 CS100J February 28, 2006 Loops, iterative statements, or repetitive statements A bit about graphics From news.com, on 23 February 2006, about browser.
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.
Start. More Loops 03/14/11 Look at geometric series example.
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 27 March 2007 Algorithms on arrays Reading: 8.3–8.5 Please punctuate this: Dear John, I want a man who knows what love is all about you are generous.
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.
1 Section 2.5 Integers and Algorithms. 2 Euclidean algorithm for finding gcd Where L is a larger number, and S is a smaller number, to find gcd(L,S):
1 Recitation 7. Developing loops Introduction. This recitation concerns developing loops using their invariants and bound functions. Your recitation instructor.
1 Assignment 6. Developing Loops Due on Tuesday, 30 October, by midnight (submitted electronically). Note that each question will be graded on the following.
Lecture 4 Discrete Mathematics Harper Langston. Algorithms Algorithm is step-by-step method for performing some action Cost of statements execution –Simple.
Introduction to Programming (in C++) Data and statements Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
1 CS1110, 20 Oct. 2009, Lec 14 Elementary graphics; intro to loops and for-loops Reading: Sec and chapter 7 on loops. The lectures on the ProgramLive.
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:
1 “Not all recursive solutions are better than iterative solutions…” “… recursion, however, can provide elegantly simple solutions to problems of great.
Mathematics in OI Prepared by Ivan Li. Mathematics in OI Greatest Common Divisor Finding Primes High Precision Arithmetic Partial Sum and Differencing.
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.
C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat.
Computer Arithmetic and the Arithmetic Unit Lesson 2 - Ioan Despi.
Programming Training Main Points: - Problems with repetitions. - Discuss some important algorithms.
Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
6 October 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
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.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
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.
Holt Algebra Properties of Logarithms Use properties to simplify logarithmic expressions. Translate between logarithms in any base. Objectives.
1 CS100J 31 October 2006 Arrays: searching & sorting. Reading: 8.5 Merry Christmas!! On Halloween? Searching and sorting algorithms are on the course website.
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Mar 3, 2006.
Algorithm Design.
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.
Advanced Program Design. Review  Step 1: Problem analysis and specification –Specification description of the problem’s inputs and output –Analysis generalize.
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Feb 18, 2005.
CS November 2010 Developing array algorithms. Reading: Haikus (5-7-5) seen on Japanese computer monitors Yesterday it worked. Today it is.
1 CS April 2010 while loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim 2. Thursday evening, 7:30PM Watch.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
While Loops 10/14/13. Repetition Allows a program to do tasks over and over. Very powerful because the computer is fast and accurate.
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.
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.
CS100J October 21, 2003 For Loops Reading: Secs Quote for the Day: Perhaps the most valuable result of all education is the ability to make yourself.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2015.
C++ Iterative Constructs
Correctness issues and Loop invariants
Start reading Sec and chapter 7 on loops
Department of Computer Science
CS100J 30 October 2007 Algorithms on arrays Reading: 8.3–8.5
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.
Counted Loops.
Big-Oh and Execution Time: A Review
Developing Loops from Invariants
CS October 2008 The while loop and assertions
Outline Altering flow of control Boolean expressions
Programming Training Main Points: - Problems with repetitions.
Asymptotic complexity Searching/sorting
Output Variables {true} S {i = j} i := j; or j := i;
Binary Search and Loop invariants
Asymptotic complexity
Developing loops from invariants
Linear and Binary Search
CS100J Nov 04, 2003 Arrays. Reading: 8
Lecture 4 Multiplication and division
Developing Loops from Invariants
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 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 do the thing you have to do, when it ought to be done, whether you like it or not; it is the first lesson that ought to be learned; and however early a man's training begins, it is probably the last lesson that he learns thoroughly. Thomas Henry Huxley Technical Education, 1877

2 Find the quotient q and remainder y when x is divided by y // pre: x ≥ 0 and y > 0 int r= x; int q= 0; // invariant: x = q*y + r and 0 ≤ r while (r >= y) { q= q + 1; r= r – y; } // post: x = q*y + r and 0 ≤ r < y Notes: 1. the postcondition defines the properties of quotient q and remainder r. 2. The invariant was found from the postcondition by seeing that all by r < y was easily truthified by setting r to x and q to 0 —but relation r < y then need not be true (so it was deleted). 3. We didn’t make progress with r= r – 1; because we can’t then fix the invariant. 4. The following relation helps us see that the invariant is kept true: q*y + r = (q+1)*y + (r–y)

3 Linear algorithm to compute a**b // pre: b ≥ 0 double z= 1; int k= 0; // invariant: z = a**k while (k != b) { z= z * a; k= k+1; } // post: z = a**b 1. The loop process the range of integers 0..b–1. For each one, it multiplies z by a. 2. Execution of the loop requires b iterations. This can be a huge number. To compute 2**32767 requires iterations! On the next slide, we show a different algorithm that requires much fewer iterations.

4 “logarithmic” algorithm to compute a**b // pre: b ≥ 0 double z= 1; double x= a; int y= b; // invariant: z*x**y = a**b // and y ≥ 0 while (y != 0) { if (y % 2 == 0) {x= x*x; y= y/2;} else {z= z*x; y= y–1;} } // post: z = a**b 1. In the case y is even, we see that the invariant is kept true because of this formula: x**y = (x*x)**(y/2) 2. In the case y is odd, we see that the invariant is kept true because of this formula: z*x**y = z*x*x**(y–1) = 2**15 – 1 in binary is (15 1’s) So, calculating **32767 needs only 30 iterations of the loop! The algorithm on the previous slide requires iterations. 3. The loop processes the bits of the binary representation of y. Each bit is processed at most twice: Example: calculate a**7 7 is 111 in binary: 111  110  11  10  1  0

5 The for-loop // precondition: n >= 0 int k= 1; x= 0; // inv: x = sum of 1..(k-1) while (k != n) { x= x + k; k= k+1; } // postcondition: x = sum of 1..(n-1) k is a loop counter. Initialized before the loop and changed only in the last statement of the repetend of the while-loop

6 The for-loop: an abbreviation of a while loop with a loop counter // pre: n >= 0 int k= 1; x= 0; // inv: x = sum of 1..(k-1) while (k != n) { x= x + k; k= k+1; } // post: x = sum of 1..(n-1) k is a loop counter. Initialized before loop; changed only in last statement of the repetend of the while-loop // pre: n >= 0 x= 0; // inv: x = sum of 1..(k-1) for (int k = 1; k != n; k= k+1) { x= x + k; } // post: x = sum of 1..n-1 for-loop collects, within the parentheses following for, everything to do with initialization of loop counter, stopping, and making progress.

7 Syntax and semantics of the for loop k is a loop counter. Initialized before the loop and not changed in the repetend of the for-loop. x= 0; // inv: x = sum of 1..(k-1) for (int k = 1; k != 20; k= k+1) { x= x + k; } // x = sum of for ( initialization ; condition ; progress ) { repetend } meaning of for-loop: initialization while ( condition ) { repetend progress }

8 Find the quotient q and remainder y when x is divided by y // pre: x ≥ 0 and y > 0 int r= x; int q= 0; // invariant: x = q*y + r and 0 ≤ r while (r >= y) { q= q + 1; r= r – y; } // post: x = q*y + r and 0 ≤ r < y // pre: x ≥ 0 and y > 0 int q= 0; // invariant: x = q*y + r and 0 ≤ r for (int r= x; r >= y; r= r – y) { q= q + 1; } // post: x = q*y + r and 0 ≤ r < y r is a loop counter. Initialized before the loop and changed only in the last statement of the repetend of the while-loop

9 Scope of the counter of a for-loop x= 0; int k; // inv: x = sum of 1..(k-1) for ( k = 1; k != 20; k= k+1) { x= x + k; } // x = sum of System.out.println(k); x= 0; int k= 1; // inv: x = sum of 1..(k-1) while (k != 20) { x= x + k; k= k + 1; } // x = sum of System.out.println(k); Illegal: Scope of k is only the loop.

10 A for-loop schema to process a range of integers // Process m..n // inv: m..k-1 has been processed and m <= k <= n+1 for (int k= m; k <= n; k= k+1) { Process k; } // Post: m..n has been processed

11 Use the schema to count number of ‘e’s in a string // Process m..n // inv: m..k-1 processed, m <= k <= n+1 for (int k= m; k <= n; k= k+1) { Process k; } // Post: m..n processed // Store in x the no. of ‘e’s in s[0..s.length()-1] // inv: m..k-1 processed, m <= k <= n+1 x = no.of ‘e’s in s[0..k-1] for (int k= m; k <= n; k= k+1) { Process k; } // Post: m..n processed Example: s = “sequoia”, x = 1 s = “defense”, x = 2 m: n:

12 Which loop condition do you like better? And why? // Process 0..n k= 0; // inv: 0..k-1 processed, 0 <= k <= n+1 while (k <= n) { Process k; k= k+1; } // post: 0..n processed // Process 0..n k= 0; // inv: 0..k-1 processed, 0 <= k <= n+1 while (k != n+1) { Process k; k= k+1; } // post: 0..n processed