Home
Add Document
Sign In
Create An Account
Lecture 21. Loop Invariants
Lecture 21 Loop Invariants Announcements for This Lecture Assignments Prelim 2 • A5 graded by weekend • Today 7:30-9pm § Grade...
Author:
Horatio Murphy
1 downloads
0 Views
226KB Size
Report
Download PDF
Recommend Documents
Loop invariants on demand
On the Automatic Detection of Loop Invariants
Loop invariants: analysis, classification, and examples
Finding Loop Invariants by Static Program Analysis
Sum. CS 361, Lecture 6. Sum Invariant. Today s Outline. Loop Invariants. Reverse
LECTURE 11: Loop Antennas
LECTURE 11: Loop Antennas Introduction
3-manifold invariants derived from link invariants
Ethnobotany. Lecture 21
Physic 231 Lecture 21
Theoretical Statistics. Lecture 21
Lecture 21 November 13
Lecture 21 Cancer Genetics I
Lecture 14 October 21, 2014
Lecture 21: Silicon wafer manufacturing
Lecture 21 Suspended Sediment Transport
EE 435. Lecture 17. Two-Stage Op Amp with LHP Zero Loop Gain - Breaking the Loop
Set zur Durchverdrahtung loop-in loop-out set. Montageanleitung S. 2 mounting instructions p. 21
LOOP-Baureihe LOOP-Range
Lecture 21: Common Collector (Emitter Follower) Amplifier
Project Management. Lecture March 21, 2013
Lecture 21: Nutrition for Children and Adolescents
Control Flow February 21, Lecture 10
Lecture 21 Pipelining Hazards, Branches, Modern
Lecture 21
Loop Invariants
Announcements for This Lecture
Assignments
Prelim 2
• A5 graded by weekend
• Today 7:30-9pm
§ Graders needed a break
§ Also working on exam
§ A–Q (Kennedy 1116)
§ R–T (Warren 131)
§ U–Z (Warren 231)
• Should be working on A6
§ § § §
11/6/12
Due week from Thursday
Start tomorrow at latest
Work on a method a day
Should start stenography no later than Sunday
• Make-ups on Wednesday
§ Should have been contacted
§ If not, see me now!
• Graded Wednesday night
§ Too much to do Today
Loop Design
2
While-Loops and Flow
print 'Before while'
Output:
count = 0 i = 0
Before while Start loop 0 End loop Start loop 1 End loop Start loop 2 End loop After while
while i < 3: print 'Start loop '+`i` count = count + i i = i + 1 print 'End loop ' print 'After while'
11/6/12
Loop Design
3
Some Important Terminology
• assertion: true-false statement placed in a program to assert that it is true at that point
§ Can either be a comment, or an assert command
• precondition: assertion placed before a statement
§ Same idea as function precondition, but more general
• postcondition: assertion placed after a statement
• loop invariant: assertion supposed to be true before and after each iteration of the loop
§ Distinct from attribute invariant
• iteration of a loop: one execution of its repetend
11/6/12
Loop Design
4
Some Important Terminology
• assertion: true-false statement placed in a program to assert that it is true at that point
§ Can either be a comment, or an assert command
s p o • precondition: assertion placed before a statement
g lo
n i n g i s but more general
§ Same idea as function precondition, e d r o f y gplaced after a statement
o l o • postcondition: tassertion d o h e m s e v i • loop G invariant: assertion supposed to be true before and after each iteration of the loop
§ Distinct from attribute invariant
• iteration of a loop: one execution of its repetend
11/6/12
Loop Design
5
Assertions versus Asserts
• Assertions prevent bugs
# x is the sum of 1..n
§ Help you keep track of what you are doing
Comment form of the assertion.
• Also track down bugs
§ Make it easier to check belief/code mismatches
• Do not confuse w/ asserts
§ All asserts are assertions
§ But reverse is not true
§ Cannot always convert a comment to an assert
11/6/12
Loop Design
x
?
n
1
x
?
n
3
x
?
n
0
6
Preconditions & Postconditions
n 1 2 3 4 5 6 7 8
precondition
# x = sum of 1..n-1 x = x + n n = n + 1 # x = sum of 1..n-1
x contains the sum of these (6)
n 1 2 3 4 5 6 7 8
postcondition
• Precondition: assertion placed before a segment
• Postcondition: assertion placed after a segment
11/6/12
x contains the sum of these (10)
Relationship Between Two
If precondition is true, then postcondition will be true
Loop Design
7
Solving a Problem
precondition
# x = sum of 1..n
n = n + 1 # x = sum of 1..n
What statement do you put here to make the postcondition true?
postcondition
A: x = x + 1
B: x = x + n
C: x = x + n+1
D: None of the above
E: I don’t know
11/6/12
Loop Design
8
Solving a Problem
precondition
# x = sum of 1..n
n = n + 1 # x = sum of 1..n
What statement do you put here to make the postcondition true?
postcondition
A: x = x + 1
B: x = x + n
C: x = x + n+1
D: None of the above
E: I don’t know
11/6/12
Remember the new value of n
Loop Design
9
Invariants: Assertions That Do Not Change
• Loop Invariant: an assertion that is true before and after each iteration (execution of repetend)
x = 0; i = 2
while i
Suggest Documents
Loop invariants on demand
Read more
On the Automatic Detection of Loop Invariants
Read more
Loop invariants: analysis, classification, and examples
Read more
Finding Loop Invariants by Static Program Analysis
Read more
Sum. CS 361, Lecture 6. Sum Invariant. Today s Outline. Loop Invariants. Reverse
Read more
LECTURE 11: Loop Antennas
Read more
LECTURE 11: Loop Antennas Introduction
Read more
3-manifold invariants derived from link invariants
Read more
Ethnobotany. Lecture 21
Read more
Physic 231 Lecture 21
Read more
Theoretical Statistics. Lecture 21
Read more
Lecture 21 November 13
Read more
Lecture 21 Cancer Genetics I
Read more
Lecture 14 October 21, 2014
Read more
Lecture 21: Silicon wafer manufacturing
Read more
Lecture 21 Suspended Sediment Transport
Read more
EE 435. Lecture 17. Two-Stage Op Amp with LHP Zero Loop Gain - Breaking the Loop
Read more
Set zur Durchverdrahtung loop-in loop-out set. Montageanleitung S. 2 mounting instructions p. 21
Read more
LOOP-Baureihe LOOP-Range
Read more
Lecture 21: Common Collector (Emitter Follower) Amplifier
Read more
Project Management. Lecture March 21, 2013
Read more
Lecture 21: Nutrition for Children and Adolescents
Read more
Control Flow February 21, Lecture 10
Read more
Lecture 21 Pipelining Hazards, Branches, Modern
Read more
×
Report "Lecture 21. Loop Invariants"
Your name
Email
Reason
-Select Reason-
Pornographic
Defamatory
Illegal/Unlawful
Spam
Other Terms Of Service Violation
File a copyright complaint
Description
×
Sign In
Email
Password
Remember me
Forgot password?
Sign In
Login with Google
Login with Facebook