10E. Summary

A good starting point for writing a function definition that involves a loop is to solve one or two examples by hand. Do a pre-simulation of your work, using any variables that you need.

Once you have some sample hand simulations, determine how variables are initialized and how they are updated. Determine how you know when the loop should stop. Then put the function definition together using those components.

Loop invariants are useful, but most students have difficulty understanding them. As you learn more, you might find that they begin to make sense. At that point, you are well on your way to becoming an expert.

Loops provide many opportunities for making mistakes, so it is important to approach them in a disciplined way.