Digression on Loop Invariants CS-2303, C-Term Digression on Loop Invariants CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel)
Digression on Loop Invariants CS-2303, C-Term 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
Digression on Loop Invariants CS-2303, C-Term 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!
Digression on Loop Invariants CS-2303, C-Term Thinking Through the Calendar Assignment int startingDay; /* init for your year*/ for (int month = 0; month < 12; month++) { }// for month This variable is “global” to the loop. I.e., it is remembered from one iteration to the next.
Digression on Loop Invariants CS-2303, C-Term Calendar Assignment (continued) int month, date; int startingDay; /* init from your year*/ for (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 to update startingDay for the next month. This is the beginning of a Loop Invariant!
Digression on Loop Invariants CS-2303, C-Term 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
Digression on Loop Invariants CS-2303, C-Term 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.
Digression on Loop Invariants CS-2303, C-Term 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?
Digression on Loop Invariants CS-2303, C-Term 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
Digression on Loop Invariants CS-2303, C-Term 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?
Digression on Loop Invariants CS-2303, C-Term 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
Digression on Loop Invariants CS-2303, C-Term 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?
Digression on Loop Invariants CS-2303, C-Term 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) printf("\n"); }// for month What about the original loop invariant for this loop?
Digression on Loop Invariants CS-2303, C-Term 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) printf("\n"); startingDay = dayOfWeek; }// for month
Digression on Loop Invariants CS-2303, C-Term Questions?