Digression on Loop Invariants

Slides:



Advertisements
Similar presentations
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Advertisements

Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic Semantics.
Objectives In this chapter, you will learn about:
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Display a 12-Month Calendar CS-2301 D-term Programming Assignment #2 12-Month Calendar CS-2301 System Programming C-term 2009 (Slides include materials.
Discussion of Assignment #2 CS-2301, B-Term Discussion of Assignment #2 CS-2301, System Programming for Non-Majors (Slides include materials from.
Introduction to FunctionsCS-2301 D-term Introduction to Functions CS-2301 System Programming C-term 2009 (Slides include materials from The C Programming.
CS Data Structures Appendix 1 How to transfer a simple loop- expression to a recursive function (factorial calculation)
Loose endsCS-2301, B-Term “Loose Ends” CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd.
"Loose ends"CS-2301 D-term “Loose Ends” CS-2301 System Programming C-term 2009 (Slides include materials from The C Programming Language, 2 nd edition,
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
More on Operator Overloading CS-2303, C-Term More on Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The.
Introduction to FunctionsCS-2301 B-term Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The.
Digression on Loop Invariants CS-2303, C-Term Digression on Loop Invariants CS-2303, System Programming Concepts (Slides include materials from The.
Scope Rules and Storage Types CS-2303, C-Term Scope Rules and Storage Types CS-2303, System Programming Concepts (Slides include materials from The.
A Deeper Look at Classes CS-2303, C-Term A Deeper Look at Classes CS-2303 System Programming Concepts (Slides include materials from The C Programming.
Conditionals, Loops, and Other Statements CS-2301 D-term Conditionals, Loops, and Other Kinds of Statements CS-2301 System Programming C-term 2009.
Miscellaneous topicsCS-2301 B-term Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.
Symbolic Constants in CCS-2303, C-Term Digression – Symbolic Constants in C CS-2303, System Programming Concepts (Slides include materials from The.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term Templates (again) CS-2303 System Programming Concepts (Slides.
Assignment #2, 12- month Calendar CS-2301, B-Term Programming Assignment #2 12-Month Calendar CS-2301, System Programming for Non-Majors (Slides.
Conditionals, Loops, and Other Statements CS-2301, B-Term Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors.
Recursion and Implementation of Functions
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
Introduction to FunctionsCIS 1057 Fall Introduction to Functions CIS 1057 Computer Programming in C Fall 2013 (Acknowledgement: Many slides based.
Chapter 5: Looping. Using the while Loop Loop – A structure that allows repeated execution of a block of statements Loop body – A block of statements.
CS161 Introduction to Computer Science
Conditionals, Loops, and Other Kinds of Statements
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Programming Assignment #4 Binary Trees in C++
CSS161: Fundamentals of Computing
More about Numerical Computation
Unary Operators ++ and --
Makefiles and Notes on Programming Assignment PA2
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
“Under the Hood” of Polymorphism
Classes, Constructors, etc., in C++
Containers and the Standard Template Library (STL)
Miscellaneous C++ Topics
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
Structures, Unions, and Typedefs
Arrays and Pointers in C & C++
Derived Classes in C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Linked Lists in C and C++
Binary Trees (and Big “O” notation)
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Bit Fields & Bitwise Operations
Programming Assignment #1 12-Month Calendar—
Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
History and Background
Symbolic Constants in C
Recursion and Implementation of Functions
Programming Assignment #6
Accessing Files in C Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Scope Rules and Storage Types
Programming Assignment #5
Differences between Java and C
Elements of a Python Program
Debuggers and Debugging
Your first C and C++ programs
Iterators Professor Hugh C. Lauer CS-2303, System Programming Concepts
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Functions in C and C++ CS-2303 System Programming Concepts Hugh C. Lauer (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
A Deeper Look at Classes
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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