CSC236H1 Lecture Notes - Lecture 15: Postcondition, Precondition, Loop Invariant
Document Summary
Correctness of iterative programs: termination: p re t erm, partial correctness p re t erm p ost, precondition: p re_cond, postcondition: p ost_cond. def iter_prog(r_1,,r_n): Li induction steps while loop_cond: { pre and steps. #post_cond pre atermination and termination is post and. Example proving the loop invariant so o six c tenca since by pre and. In i a i i so since another iteration of the whitehoop exists we have ie e cenca. So in i e cenca so 0 e i ke e cenca ice elen a ca suma. E acj i o a cia by line a. Example proving partial correctness: precondition: a is a non-empty list of numbers, postcondition: returns the average of the numbers in a. def avg(a): Li(k): if the loop is executed at least k times, then: (a) sk = qik 1 j=0 a[j]; (b) 0 ik len(a). Also lz ct it e cenca ik clenck holds ik uncas iterations.