Presentation is loading. Please wait.

Presentation is loading. Please wait.

Digression on Loop Invariants CS-2303, C-Term 20101 Digression on Loop Invariants CS-2303, System Programming Concepts (Slides include materials from The.

Similar presentations


Presentation on theme: "Digression on Loop Invariants CS-2303, C-Term 20101 Digression on Loop Invariants CS-2303, System Programming Concepts (Slides include materials from The."— Presentation transcript:

1 Digression on Loop Invariants CS-2303, C-Term 20101 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)

2 Digression on Loop Invariants CS-2303, C-Term 20102 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

3 Digression on Loop Invariants CS-2303, C-Term 20103 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!

4 Digression on Loop Invariants CS-2303, C-Term 20104 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.

5 Digression on Loop Invariants CS-2303, C-Term 20105 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!

6 Digression on Loop Invariants CS-2303, C-Term 20106 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

7 Digression on Loop Invariants CS-2303, C-Term 20107 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.

8 Digression on Loop Invariants CS-2303, C-Term 20108 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?

9 Digression on Loop Invariants CS-2303, C-Term 20109 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

10 Digression on Loop Invariants CS-2303, C-Term 201010 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?

11 Digression on Loop Invariants CS-2303, C-Term 201011 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

12 Digression on Loop Invariants CS-2303, C-Term 201012 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?

13 Digression on Loop Invariants CS-2303, C-Term 201013 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?

14 Digression on Loop Invariants CS-2303, C-Term 201014 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

15 Digression on Loop Invariants CS-2303, C-Term 201015 Questions?


Download ppt "Digression on Loop Invariants CS-2303, C-Term 20101 Digression on Loop Invariants CS-2303, System Programming Concepts (Slides include materials from The."

Similar presentations


Ads by Google