When we write a program, we are describing a series of steps using a formal programming language with a very specific syntax. In a sense, a program is like a recipe for cooking a particular dish, or perhaps the instructions for putting a child’s new toy together that comes “some assembly required.”
In a program, assembly instructions, or a recipe, there are four basic patterns of steps:
Normally, steps happen in order. Once one step is completed, we move on to the next step; sometimes the steps are even numbered. An example of this might be: (1) add 300 grams of flour, (2) add 100 ml of milk, (3) add one egg.
Sometimes we skip a step. Depending on some condition, we decide to do a step or not do a step. An example of a conditional step in our recipe might be: (4) if you are cooking at more than 2000 meters above sea level, add a second egg. Often these conditional steps are expressed using the word “if.”
Sometimes we do a step over and over until some condition is satisfied. An example of a repeated step might be: (5) blend the mixture with a fork until nearly all the lumps are gone. Words like “while,” “repeat,” or “until” are often used to describe repeated steps.
Sometimes we end up with a set of steps that we will want to do over and over again the same way each time. Instead of repeatedly describing the steps each time they are needed, we describe them once and store them for later use. ...