CS-2303, System Programming Concepts - PowerPoint PPT Presentation

About This Presentation
Title:

CS-2303, System Programming Concepts

Description:

Digression on Loop Invariants CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie ... – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 16
Provided by: Hug76
Learn more at: http://web.cs.wpi.edu
Category:

less

Transcript and Presenter's Notes

Title: CS-2303, System Programming Concepts


1
Digression 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)

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

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!

4
Thinking Through the Calendar Assignment
  • int startingDay / init for your year/
  • for (int month 0 month lt 12 month)
  • // for month

5
Calendar 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!
6
Calendar 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

7
Calendar 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

8
Calendar 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

9
Calendar 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

10
Calendar 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

11
Calendar 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

12
Calendar 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

13
Calendar 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

14
Calendar 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

15
Questions?
Write a Comment
User Comments (0)
About PowerShow.com