Download presentation
Presentation is loading. Please wait.
1
(We’ll spend a few lectures on iteration)
CS100A, Lecture 8, 24 Sept 1997 Iteration (We’ll spend a few lectures on iteration) Reading: Holmes, Chapter 4, p Iterate: Reiterate Reiterate: to say or do over again or repeatedly; repeat often or continually, sometimes with a wearying effect. Iteration: the action of repeating or reiterating: repetition, reiteration. CS100A, Lecture 8, 24 Sept. 1998
2
Views on Iteration, Repetition
Like warmed-up cabbage served at each repast, The repetition kills the wretch at last. Juvenal, Satires Use not vain repetitions, as the heathens do. Matthew vi, 7 Oh, thou hast damnable iteration, and art indeed able to corrupt a saint. Shakespeare, Henry IV Iteration, like friction, is likely to generate heat instead of progress. George Eliot, The Mill on the Floss A languid, leaden iteration reigns, And ever must, o’er those whose joys are joys Of sight, smell, taste Young, Night Thoughts CS100A, Lecture 8, 24 Sept. 1998
3
// Write out the numbers 0..9, each on a // separate line
System.out.println(0); System.out.println(1); System.out.println(2); System.out.println(3); System.out.println(4); System.out.println(5); System.out.println(6); System.out.println(7); System.out.println(8); System.out.println(9); The while loop allows us to do this more tersely. i= 0; while (i<10) { System.out.println(i); i= i+1; } CS100A, Lecture 8, 24 Sept. 1998
4
Syntax of the Java while loop:
while ( <boolean expression> ) statement called the “body” called the of the loop “condition” Example: i= 0; // General picture: integers 0..i-1 have been // printed while (i<10) { System.out.println(i); i= i+1; } CS100A, Lecture 8, 24 Sept. 1998
5
Execution of the while loop while ( B ) flowchart S
Execution of a while loop: Evaluate condition B; if it is true, execute body S and repeat the process. The first execution of S is called iteration 0. The second execution of S is called iteration 1. The third execution of S is called iteration 2. ... Evaluate B Execute S true false CS100A, Lecture 8, 24 Sept. 1998
6
// General picture: s= 1 + 2 + … + i-1. while (i != 5)
Another example: // Store in s s= 0; i= 1; // General picture: s= … + i-1. while (i != 5) {s= s + i; i= i+1;} Execution: i | ? | 1 | | 2 | | 3 | | 4 | | 5 | s | 0 | | 1 | | 3 | | 6 | | 10 | | Execute on computer. CS100A, Lecture 8, 24 Sept. 1998
7
s= 0; i= 1; // Initialization // General picture: s = 1 + 2 + … + i-1.
// Store in s s= 0; i= 1; // Initialization // General picture: s = … + i-1. while (i != 5) {s= s + i; i= i+1;} The “General picture”, or “invariant” is a relation that shows how the values of the variables are related just before and just after each iteration. It gives the meaning of the variables before and after each iteration. It helps us understand what the loop is doing. For each loop, write an invariant, which shows the state of affairs before and after each iteration. The invariant gives the meaning of the variables during execution of the loop. The initialization “truthifies” the invariant (makes it true). Each iteration “maintains” the invariant (keeps it true). Condition is written so that when the loop terminates, the general picture helps us see that the desired answer has been obtained. CS100A, Lecture 8, 24 Sept. 1998
8
Note the indentation used below: 1 // Store 1 + 2 + 3 + 4 in s
s= 0; i= 1; // Invariant: s = … + i-1 while (i != 5) { 5 s= s + i; 6 i= i+1; 7 } Line 1 is a statement-comment: a statement to be executed that it is written in English, so it is a comment. The statement-comment says WHAT to do. Lines 2-7 are the implementation of the statement comment. They are indented beneath it. Lines 5-7 are the body of the while loop; they form a substatement of the loop. They are indented. Rule: Indent substatements of a statement! CS100A, Lecture 8, 24 Sept. 1998
9
Develop another algorithm for same task
// Store in s Use invariant s= … + i. To make it true, use initialization: s= 0; i= 0; When will s = be true? When i=4. Use condition i != 4 Body of loop: Make “progress” by increasing i and changing s to maintain the invariant: i= i+1; s= s+i; Loop: s= 0; i= 0; // Invariant: s = while (i !=4) {i= i+1; s= s+i;} Invariant is different, so loop is different! CS100A, Lecture 8, 24 Sept. 1998
10
// Print the factors of integer n int i= 1;
Another Example: (Assume that n has been declared and assigned a variable before the following program segment is to be executed.) // Print the factors of integer n int i= 1; // Invariant: the factors of n that are less than // i have been printed, and 1 <= i <= n+1 while (i <= n) { if (n % i == 0) System.out.println(i); i= i+1; } x % y yields the remainder when x is divided by y. Example: 13 % 1 = 0 13 % 2 = 1 13 % 3 = 1 13 % 5 = 3 CS100A, Lecture 8, 24 Sept. 1998
11
MEMORIZE THE DEFINITIONS
OF THESE TERMS! Iterate. Slide 1 While loop. Slide 3 Body of a while loop. Slide 3 Condition of a while loop. Slide 3 How to execute a while loop. Slide 5 Iteration of a while loop. Slide 5 Invariant of a loop. Slide 7 A statement “truthifies” a relation. Slide 7 A statement “maintains” a relation. Slide 7 Statement-comment. Slide 8 Implementation of a statement-comment. Slide 8 CS100A, Lecture 8, 24 Sept. 1998
12
You MUST know precise meanings of following! Type (e.g. int, bool)
PRELIM MONDAY EVENING You MUST know precise meanings of following! Type (e.g. int, bool) Class Subclass Field (of a class) or instance variable Static field (of a class) or class variable Instance of a class –also called an object Method function procedure constructor Local variable (of a method) Parameter (of a method) Method call –how each of the three kinds of methods is called Argument (of a method call) How to execute an assignment, if statement. How to execute a method call How to evaluate expression new C(…) CS100A, Lecture 8, 24 Sept. 1998
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.