First, if the students have any questions answer them. Go over exercise 4 of chapter 2 of the textbook (page 81) slowly and make sure the students understand every step. Here is the solution: Termination: Let E = l + 1 - i. E is definitely an integer because l and i are both integers. Sine f <= l by the precondition: E_0 = l + 1 - i_0 = l + 1 - f >= 1, and since i_k <= l at the begining of each iteration of the loop, i_{k+1} = i_k + 1, i_{k+1} <= l + 1, so E_{k+1} = l + 1 - i_{k+1} >=0 So E_k is always a natural number. Furthermore, if there is an iteration number k + 1 of the loop, then E_{k+1} = l + 1 - i_{k+1} = l + 1 - (i_k + 1) = l + 1 - i_k - 1 = E_k - 1 < E_k Therefore, E_0 > E_1 > ... is a decreasing sequence of natural numbers and is finite, which implies that the loop terminates. Partial correctness: Under the assumption that the precondition holds, we prove the following loop invariant (LI) by induction on the number of iterations performed: "i <= l + 1 and t is the number of ccurences of x in A[f...(i - 1)]". Base: t_0 = t = 0 and A[f...(i_0 - 1)] = A[f...(f - 1)] which is the empty array by convention. Also, i_0 = f <= l < l + 1 by the precondition. Ind. Step: Assuming that LI_k is true, we want to prove that LI_{k+1} holds too. Case 1: If there is no iteration number k + 1, then LI_{k+1} is equivalent to LI_k so it is true by the Ind. Hyp. Case 2: If there is an iteration number k + 1, then that means i_k <= l (by the loop test), so i_{k+1} = i_k + 1 <= l + 1. Now we consider two subcases: Subcase A: If A[i_k] =/= x, then the test "A[i] = x" fails and t_{k+1} = t_k. Since t_k is the number of occurences of x in A[f...(i_k - 1)] by the Ind. Hyp. and A[i_k] =/= x, t_{k+1} = t_k is the number of occurences of x in A[f...i_k] which is the same as A[f...(i_{k+1} - 1)]. Subcase B: If A[i_k] = x, then the test "A[i] = x" succeeds and t_{k+1} = t_k + 1. Since t_k is the number of occurences of x in A[f...(i_k - 1)] by the Ind. Hyp. and A[i_k] = x, t_{k+1} = t_k + 1 is the number of occurences of x in A[f...i_k] = A[f...(i_{k+1} - 1)]. In both cases, LI_{k+1} is true. Hence, by induction, LI holds after every iteration of the loop. When the loop terminates, we know that i > l and by LI: i <= l + 1. This implies that i = l + 1. Also, LI says that t is the number of occurences of x in A[f...(i - 1)] = A[f...l], which is exactly the postcondition.