Presentation is loading. Please wait.

Presentation is loading. Please wait.

Digression on Loop Invariants

Similar presentations


Presentation on theme: "Digression on Loop Invariants"— Presentation transcript:

1 Digression on Loop Invariants
Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie, Absolute C++, by Walter Savitch, The C++ Programming Language, Special Edition, by Bjarne Stroustrup, and from C: How to Program, 5th and 6th editions, by Deitel and Deitel) CS-2303, A-Term 2012 Loop Invariants

2 Definition – Loop Invariant
Something that is true at a certain point of each iteration of the loop Often at start of iteration E.g., a relationship of the variables Expressed as a logical statement called an assertion Needs to be preserved from one iteration to the next Does not necessarily remain true within loop body, but only at the specific point each time through CS-2303, A-Term 2012 Loop Invariants 2

3 Loop Invariant Allows you to reason about a program …
… and to convince yourself (and other people) that it is correct For many “ordinary” programs, you do this subconsciously For seriously difficult programs, you must write out the loop invariants! CS-2303, A-Term 2012 Loop Invariants

4 Thinking Through the Calendar Assignment
This variable is “global” to the loop. I.e., remembered from one iteration to the next. int startingDay; /* init for your year*/ for (int month = 0; month < 12; month++) { } // for month CS-2303, A-Term 2012 Loop Invariants 4

5 Calendar Assignment (continued)
int startingDay; /* init for your year*/ for (int month = 0; month < 12; month++) { } // for month At beginning of each iteration, startingDay indicates the day of the week on which that particular month starts. It is the responsibility of the loop body to update startingDay for the next month. This is the beginning of a Loop Invariant! CS-2303, A-Term 2012 Loop Invariants 5

6 Calendar Assignment (continued)
int month, date; int startingDay; /* init from user input*/ for (month = 0; month < 12; month++) { const int daysInMonth = …; /* set # of days */ int dayOfWeek = 0; printf(…); //month name printf(…); //days of week } // for month CS-2303, A-Term 2012 Loop Invariants 6

7 Calendar Assignment (continued)
int month, date; int startingDay; /* init from user input*/ for (month = 0; month < 12; month++) { const int daysInMonth = …; /* set # of days */ int dayOfWeek; printf(…); //month name printf(…); //days of week for (dayOfWeek = 0; dayOfWeek<startingDay; dayOfWeek++) printf(/*blanks*/); … } // for month Note that dayOfWeek is global to this loop. It is remembered outside this inner loop. CS-2303, A-Term 2012 Loop Invariants 7

8 Calendar Assignment (continued)
int month, date; int startingDay; /* init from user input*/ for (month = 0; month < 12; month++) { const int daysInMonth = …; /* set # of days */ int dayOfWeek; printf(…); //month name printf(…); //days of week for (dayOfWeek = 0; dayOfWeek<startingDay; dayOfWeek++) printf(/*blanks*/); … } // for month What is the loop invariant associated with this loop? CS-2303, A-Term 2012 Loop Invariants 8

9 Calendar Assignment (continued)
int month, date; int startingDay; /* init from user input*/ for (month = 0; month < 12; month++) { const int daysInMonth = …; /* set # of days */ int dayOfWeek; printf(…); //month name printf(…); //days of week for (dayOfWeek = 0; dayOfWeek<startingDay; dayOfWeek++) printf(/*blanks*/); for (int date = 1; date <= daysInMonth; date++){ } // for date } // for month CS-2303, A-Term 2012 Loop Invariants 9

10 Calendar Assignment (continued)
int month, date; int startingDay; /* init from user input*/ for (month = 0; month < 12; month++) { const int daysInMonth = …; /* set # of days */ int dayOfWeek; printf(…); //month name printf(…); //days of week for (dayOfWeek = 0; dayOfWeek<startingDay; dayOfWeek++) printf(/*blanks*/); for (int date = 1; date <= daysInMonth; date++){ } // for date } // for month What should the loop invariant be for this loop? Why? CS-2303, A-Term 2012 Loop Invariants 10

11 Calendar Assignment (continued)
int month, date; int startingDay; /* init from user input*/ for (month = 0; month < 12; month++) { const int daysInMonth = …; /* set # of days */ int dayOfWeek; printf(…); //month name printf(…); //days of week for (dayOfWeek = 0; dayOfWeek<startingDay; dayOfWeek++) printf(/*blanks*/); for (int date = 1; date <= daysInMonth; date++){ printf("…", date); if (++dayOfWeek>6) { printf("\n"); dayOfWeek = 0; }; } // for date } // for month CS-2303, A-Term 2012 Loop Invariants 11

12 Calendar Assignment (continued)
int month, date; int startingDay; /* init from user input*/ for (month = 0; month < 12; month++) { const int daysInMonth = …; /* set # of days */ int dayOfWeek; printf(…); //month name printf(…); //days of week for (dayOfWeek = 0; dayOfWeek<startingDay; dayOfWeek++) printf(/*blanks*/); for (int date = 1; date <= daysInMonth; date++){ printf("…", date); if (++dayOfWeek>6) { printf("\n"); dayOfWeek = 0; }; } // for date } // for month Is loop invariant preserved by this loop? Why? CS-2303, A-Term 2012 Loop Invariants 12

13 Calendar Assignment (continued)
int month, date; int startingDay; /* init from user input*/ for (month = 0; month < 12; month++) { const int daysInMonth = …; /* set # of days */ int dayOfWeek; printf(…); //month name printf(…); //days of week for (dayOfWeek = 0; dayOfWeek<startingDay; dayOfWeek++) printf(/*blanks*/); for (int date = 1; date <= daysInMonth; date++){ printf("…", date); if (++dayOfWeek>6) { printf("\n"); dayOfWeek = 0; }; } // for date if (dayOfWeek != 0) } // for month What about the original loop invariant for this (outer) loop? CS-2303, A-Term 2012 Loop Invariants 13

14 Calendar Assignment (continued)
int month, date; int startingDay; /* init from user input*/ for (month = 0; month < 12; month++) { const int daysInMonth = …; /* set # of days */ int dayOfWeek; printf(…); //month name printf(…); //days of week for (dayOfWeek = 0; dayOfWeek<startingDay; dayOfWeek++) printf(/*blanks*/); for (int date = 1; date <= daysInMonth; date++) { printf("…", date); if (++dayOfWeek>6) { printf("\n"); dayOfWeek = 0; }; } // for date if (dayOfWeek !=0) startingDay = dayOfWeek; } // for month CS-2303, A-Term 2012 Loop Invariants 14

15 Questions? Next Topic CS-2303, A-Term 2012 Loop Invariants


Download ppt "Digression on Loop Invariants"

Similar presentations


Ads by Google