Transcript .ppt

Announcements
•
•
•
•
Assignment P2 is due Thursday
P3 will be handed out tomorrow
Quiz today
Will have more quizzes from now on to
help clarify important topics
• For examples of programs, see TA’s
solutions to P1 on the web
CS 100
Lecture 9
1
Some Issues
• “Lectures are too fast. . .
difficult to understand.”
• “I haven’t asked the TAs for
help.”
• Responsibility for One’s
Education at the University
Level
• Please ask for help if/when
you are confused!
CS 100
Lecture 9
2
Scheduling
•
•
•
•
•
•
•
•
•
•
•
Minimal Topics Left
Iteration, while, for
loops
Arrays 1-d, 2-d
Search Algorithms
Sorting Algorithms
Matlab (>= 2 days)
Control Programs
Applets
Debugging
C (>= 2 days)
Pointers
Recursion
CS 100
Number of lectures left
(including today):
15
If you want the lectures to slow down,
or something to be explained again, you
MUST ask questions during class. . .
Otherwise:
Yadda, yadda
yadda. Blah
blah blah.
Lecture 9
3
Check this slide for
ideas of what topics
to study in the text.
Use the index.
•
•
•
•
Today’s Topics
Review
Iteration (while loops and for loops)
Invariants
break and continue statements
• Tomorrow: Arrays
CS 100
Lecture 9
4
Review
• Classes and Subclasses
• Static Variables and Methods
• Private/Public
CS 100
Lecture 9
5
Iterate
• 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.
CS 100
Lecture 9
6
Why do we like Iteration?
// 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);
CS 100
// The while loop allows us to
// do this more tersely.
i= 0;
while (i<10) {
System.out.println(i);
i= i+1;
}
Lecture 9
7
Recall: Java Syntax
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;
}
CS 100
Lecture 9
8
Example trace
// Store 1 + 2 + 3 + 4 in s
s= 0; i= 1;
// General picture: s= 0 + 1 + 2 + … + i-1.
while (i != 5)
{s= s + i; i= i+1;}
Execution trace:
i |? |1
|
s |0 |
|1
CS 100
|2
|
|
|3
|3
|
Lecture 9
|
|6
|4
|
|
| 10
|5|
| |
9
Whole program version:
public class TrivialApplication {
public static void main(String args[]) {
int s;
int i;
// Store 1 + 2 + 3 + 4 in s
s= 0; i= 1;
// General picture: s= 0 + 1 + 2 + … + i-1.
while (i != 5) {
s= s + i;
i= i+1;}
System.out.println("i = " + i + " s = " + s);
try { System.in.read(); // prevent console window from going away
} catch (java.io.IOException e) {} }}
CS 100
Lecture 9
10
Invariants
• 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.
CS 100
Lecture 9
11
Does the Invariant Hold?
// Store 1 + 2 + 3 + 4 in s
s= 0; i= 1;
// General picture: s= 0 + 1 + 2 + … + i-1.
while (i != 5)
{s= s + i; i= i+1;}
Initialization?
Each iteration?
CS 100
Lecture 9
12
Another algorithm -- same problem
// Store 1 + 2 + 3 + 4 in s
Use invariant
s= 0 + 1 + 2 + … + i.
To make it true, use initialization: s= 0; i= 0;
When will s = 1+2+3+4 be true? When i=4.
So, 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 = 1+2+3+4
while (i !=4)
{i= i+1; s= s+i;}
CS 100
Lecture 9
13
Another Example
Another Example: (Assume that n has been declared and
assigned a value 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;
}
CS 100
Lecture 9
Reminder: x % y yields
the remainder when x is
divided by y.
Example: 13 % 1 = 0
13 % 2 = 1
13 % 3 = 1
13 % 5 = 3
14
Third Example
Problem: Given n>0,
set s to the largest power of 2 that is at most n.
n s
1 1 20 = 1
2 2 21 = 2
3 2
4 4 22 = 2*2 = 4
5 4
6 4
7 4
8 8 23 = 2*2*2 = 8
CS 100
Lecture 9
15
Third example continued. . .
Strategy: Start s at 1 and continue to multiply it by 2 until the next
multiplication by 2 would make it too big.
General picture (invariant) s is a power of 2 and s <= n
Initialization: s= 1;
Stop when: 2*s > n
Continue as long as: 2*s <= n
Make progress toward termination and keep general picture true: s= 2*s;
// Known: n>0. Set s to the largest power of 2 that is at most n
s= 1;
//invariant: s is a power of 2 and s <= n
while (2*s <= n)
s= 2*s;
CS 100
Lecture 9
16
Fourth Example
Write a method with the following heading:
// Return the position of c in s (or s.length() if c is not in s)
public static int find(char c, String s)
Examples
c
s
result
‘a’
“All’s well that ends”
13
‘A’
“All’s well that ends”
0
‘i’
“All’s well that ends”
20
‘w’
“”
0
‘w’
“”
1
‘’
“”
0
CS 100
Lecture 9
17
Fourth example continued. . .
Strategy: look at the characters of s one by one, starting from the
beginning. Return as soon as c is found. Use a variable j to tell
how much of s has been “scanned” (looked at).
General picture: c is not in s[0..j-1], i.e. c is not one of the first j
characters of s, and 0 <= j <= s.length().
Initialization: j= 0;
When can the loop stop?
When j= s.length() or c = s[j].
So the loop condition is
j != s.length() && s.charAt(j) != c
CS 100
Lecture 9
18
The for statement
• Used when you want to execute a loop a
specific number of times
• Syntax:
for (initialization; condition; increment)
statement;
• Example:
for(i = 0; i < 10; i++)
System.out.println(i);
CS 100
Lecture 9
19
Diagram of a for loop
initialization
condition false
eval’d
true
statement
increment
CS 100
Lecture 9
20
The break statement
• The break statement can be used to break
out of a loop
• When a break is encountered, the execution
of the loop is stopped, and the statement
after the loop is executed
• for(num = 1; num <= 20; num++) {
if (num == 12)
break;
System.out.println(“num is ” + num);
}
System.out.println(“I’m after the loop!”);
CS 100
Lecture 9
21
The continue statement
• When a continue statement is encountered,
the rest of the current iteration is skipped,
and another iteration begins
• for(num = 1; num <= 20; num++) {
if (num == 12)
continue;
System.out.println(“num is ” + num);
}
System.out.println(“I’m after the loop!”);
CS 100
Lecture 9
22