This chapter covers
- Using boolean values in conditions.
- Running the same code multiple times using while and for-loops.
- Deciding what code to run with an if-statement.
- Looping over ranges of numbers.
- Defining functions spanning multiple lines of code.
- Implementing control flow using recursion or iteration.
Control flow is what separates a computer from a mere calculator. Calculators are for computing single expressions. Computers have the ability to repeat the same calculations with different inputs numerous times without human intervention. Computers can choose to perform one calculation over another based on whether a condition holds true or not.
In this chapter we will explore code examples oriented around producing mathematical tables. We will explore tables for trigonometric functions as such tables are well known and of historical importance.
Later we will explore conditional execution to help count the growth of rabbits using a method developed by Italian mathematician Fibonacci 800 years ago.
In the age sail the use of mathematical tables became more widespread, and a need to develop ways to automate the calculation of these tables developed. E.g. navigation is based around the calculation of angles and the sides of triangles. This means using trigonometric functions such as sine, cosine and tangent.