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

Slides:



Advertisements
Similar presentations
Program Looping EE2372 Software Design I Dr. Gerardo Rosiles.
Advertisements

CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
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:
Event-drive SimulationCS-2303, C-Term Project #3 – Event-driven Simulation CS-2303 System Programming Concepts (Slides include materials from The.
Functions and InterfacesCS-2303, C-Term Functions and Interfaces in C CS-2303, System Programming Concepts (Slides include materials from The C Programming.
File AccessCS-2301, B-Term File Access CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd.
Strings and Dynamic Memory Allocation CS-2301, B-Term Programming Assignment #6 Strings and Dynamic Memory Allocation CS-2301, System Programming.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
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.
Programming Assignment #4 Binary Trees
"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.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
Introduction to FunctionsCS-2301 B-term Introduction to Functions CS-2301, System Programming for Non-majors (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.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Structures, Unions, and Typedefs CS-2303, C-Term Structures, Unions, and Typedefs CS-2303 System Programming Concepts (Slides include materials from.
Assignment #2, 12- month Calendar CS-2301, B-Term Programming Assignment #2 12-Month Calendar CS-2301, System Programming for Non-Majors (Slides.
CS-2303 System Programming Concepts
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,
Week 1 - Friday.  What did we talk about last time?  C basics  Data types  Output with printf()
Programming Logic and Design Fifth Edition, Comprehensive
 An instruction or group of instructions.  Computer executes program repeatedly for specified number of times. Or until some terminating conditions are.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Programming Logic and Design Sixth Edition Chapter 5 Looping.
CS50 Week 2. RESOURCES Office hours ( Lecture videos, slides, source code, and notes (
1. Agenda for loop Short-handed notation How to use break and continue 2.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Bit Fields & Bitwise Operations CS-2303, C-Term Bit Fields & Bitwise Operations CS-2303 System Programming Concepts (Slides include materials from.
Introduction to FunctionsCIS 1057 Fall Introduction to Functions CIS 1057 Computer Programming in C Fall 2013 (Acknowledgement: Many slides based.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
PGT C Programming1 Week 4 – Repetition Structures / Loops.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
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
Looping.
More about Numerical Computation
Unary Operators ++ and --
Programming Languages 2nd edition Tucker and Noonan
Miscellaneous C++ Topics
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,
Symbolic Constants in C
Recursion and Implementation of Functions
Scope Rules and Storage Types
Programming Assignment #5
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.
Digression on Loop Invariants
Java Programming Loops
1-6 Midterm Review.
Programming Languages 2nd edition Tucker and Noonan
Presentation transcript:

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?