Title: CS-2303, System Programming Concepts
1Digression on Loop Invariants
- CS-2303, System Programming Concepts
- (Slides include materials from The C
Programming Language, 2nd edition, by Kernighan
and Ritchie and from C How to Program, 5th and
6th editions, by Deitel and Deitel)
2Definition 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
3Loop 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!
4Thinking Through the Calendar Assignment
- int startingDay / init for your year/
- for (int month 0 month lt 12 month)
- // for month
5Calendar Assignment (continued)
- int month, date
- int startingDay / init from your year/
- for (month 0 month lt 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!
6Calendar Assignment (continued)
- int month, date
- int startingDay / init from user input/
- for (month 0 month lt 12 month)
- const int daysInMonth
- / set of days /
- int dayOfWeek 0
- printf() //month name
- printf() //days of week
- // for month
7Calendar Assignment (continued)
- int month, date
- int startingDay / init from user input/
- for (month 0 month lt 12 month)
- const int daysInMonth
- / set of days /
- int dayOfWeek
- printf() //month name
- printf() //days of week
- for (dayOfWeek 0
- dayOfWeekltstartingDay
- dayOfWeek)
- printf(/blanks/)
- // for month
8Calendar Assignment (continued)
- int month, date
- int startingDay / init from user input/
- for (month 0 month lt 12 month)
- const int daysInMonth
- / set of days /
- int dayOfWeek
- printf() //month name
- printf() //days of week
- for (dayOfWeek 0
- dayOfWeekltstartingDay
- dayOfWeek)
- printf(/blanks/)
- // for month
9Calendar Assignment (continued)
- int month, date
- int startingDay / init from user input/
- for (month 0 month lt 12 month)
- const int daysInMonth
- / set of days /
- int dayOfWeek
- printf() //month name
- printf() //days of week
- for (dayOfWeek 0
- dayOfWeekltstartingDay
- dayOfWeek)
- printf(/blanks/)
- for (int date 1 date lt
- daysInMonth date)
- // for date
- // for month
10Calendar Assignment (continued)
- int month, date
- int startingDay / init from user input/
- for (month 0 month lt 12 month)
- const int daysInMonth
- / set of days /
- int dayOfWeek
- printf() //month name
- printf() //days of week
- for (dayOfWeek 0
- dayOfWeekltstartingDay
- dayOfWeek)
- printf(/blanks/)
- for (int date 1 date lt
- daysInMonth date)
- // for date
- // for month
11Calendar Assignment (continued)
- int month, date
- int startingDay / init from user input/
- for (month 0 month lt 12 month)
- const int daysInMonth
- / set of days /
- int dayOfWeek
- printf() //month name
- printf() //days of week
- for (dayOfWeek 0
- dayOfWeekltstartingDay
- dayOfWeek)
- printf(/blanks/)
- for (int date 1 date lt
- daysInMonth date)
- printf("", date)
- if (dayOfWeekgt6)
- printf("\n")
- dayOfWeek 0
-
- // for date
- // for month
12Calendar Assignment (continued)
- int month, date
- int startingDay / init from user input/
- for (month 0 month lt 12 month)
- const int daysInMonth
- / set of days /
- int dayOfWeek
- printf() //month name
- printf() //days of week
- for (dayOfWeek 0
- dayOfWeekltstartingDay
- dayOfWeek)
- printf(/blanks/)
- for (int date 1 date lt
- daysInMonth date)
- printf("", date)
- if (dayOfWeekgt6)
- printf("\n")
- dayOfWeek 0
-
- // for date
- // for month
13Calendar Assignment (continued)
- int month, date
- int startingDay / init from user input/
- for (month 0 month lt 12 month)
- const int daysInMonth
- / set of days /
- int dayOfWeek
- printf() //month name
- printf() //days of week
- for (dayOfWeek 0
- dayOfWeekltstartingDay
- dayOfWeek)
- printf(/blanks/)
- for (int date 1 date lt
- daysInMonth date)
- printf("", date)
- if (dayOfWeekgt6)
- printf("\n")
- dayOfWeek 0
-
- // for date
- if (dayOfWeek ! 0)
- printf("\n")
- // for month
14Calendar Assignment (continued)
- int month, date
- int startingDay / init from user input/
- for (month 0 month lt 12 month)
- const int daysInMonth
- / set of days /
- int dayOfWeek
- printf() //month name
- printf() //days of week
- for (dayOfWeek 0
- dayOfWeekltstartingDay
- dayOfWeek)
- printf(/blanks/)
- for (int date 1 date lt
- daysInMonth date)
- printf("", date)
- if (dayOfWeekgt6)
- printf("\n")
- dayOfWeek 0
-
- // for date
- if (dayOfWeek !0)
- printf("\n")
- startingDay dayOfWeek
- // for month
15Questions?