CSE 2221 Study Guide - Final Guide: Increment And Decrement Operators, Javadoc, Mathematical Induction

32 views6 pages

Document Summary

Reasoning about method calls: what a method call does is described by its contract, precondition: a property that is true before the call is made, postcondition: a property that is true after the call returns. Reasoning about loops: what a while loop does is described by its loop invariant. Simply put, experience shows this is a good way to think about loops. Yes; the same idea can be applied to for loops, but some modifications are required. The loop invariant is a property that is both true in the first incoming arrow, right before the loop begins and in the left return arrow, after every execution of the loop body. Example #1 void append(queue q: co(cid:374)(cid:272)ate(cid:374)ates (cid:894)(cid:862)appe(cid:374)ds(cid:863)(cid:895) (cid:395) to the e(cid:374)d of this, updates: this, clears: q. The value of x is not involved in the loop invariant because there is no x when we first hit the loop!