next up previous contents
Next: Iteration Up: An Introduction to Darwin Previous: The Types of Structured

Iteration and Recursion

This chapter explores the different methods available to users for looping over a segment of code. In §[*] of Chapter [*], we introduced the first such method: the for loop. We briefly revisit this construct here and show some alternative forms for this command. We introduce the second method of iterating in Darwin, the while loop, and give some short examples of its application.

We also discuss another powerful method of repeatedly executing code called recursion. When a procedure calls itself, either directly or indirectly, it is said to be making a recursive call. We show two examples of how recursion can be exploited to solve problems. The first of these computes a factorial via the divide and conquer method. The second example manipulates binary trees, a data structure used many times over in later chapters.


Gaston Gonnet