Algorithms with Decisions and Repetition
Algorithms are more than just a series of steps. There are times when we have to make decisions or when we repeat steps. Take a look at the following algorithm for steps to make a bed.
- On the right side, pull up the flat top sheet to the top of the bed.
- On the left side, pull up the flat top sheet to the top of the bed.
- On the right side, fold down the top of the flat top sheet about 12 inches.
- On the left side, fold down the top of the flat top sheet about 12 inches.
- On the right side, tuck in the sheet on the side.
- On the left side, tuck in the sheet on the side.
- On the right side, place the pillow at the top of the bed.
- On the left side, place the pillow at the top of the bed.
- On the right side, pull the comforter up and over the pillow.
- On the left side, pull the comforter up and over the pillow.
- Are there decisions that should be included?
- Are there repeative steps used in this process?
Decisions
When the computer make decisions, they are binary - yes or no. There are steps that happen when the conditions or the decision is true or yes, and sometimes steps that happen when the condition is false or no. Sometimes there are additional checks to take along the way as well.
In the algorithm above, maybe there is a decision at the start to determine whether the sheets need to be changed or not.
- Do the sheets need to be changed?
- If yes,
- Remove the comforter from the bed.
- On the right side, remove the pillow case for that pillow.
- On the left side, remove the pillow case for that pillow.
- Remove the flat sheet.
- Remove the fitted sheet.
- Get fresh sheets
- On the upper right corner, put the fitted sheet around the corner.
- On the upper left corner, put the fitted sheet around the corner.
- On the lower left corner, put the fitted sheet around the corner.
- On the lower right corner, put the fitted sheet around the corner.
- On the upper right corner, lay out the flat sheet, pulling it up to the corner.
- On the upper left corner, lay out the flat sheet, pulling it up to the corner.
- On the lower left corner, lay out the flat sheet, pulling it down around the corner straightening it out.
- On the lower right corner, lay out the flat sheet, pulling it down around the corner straightening it out.
- On the lower right corner, pull the corner tight and tuck it around the underside of the bed. Pull up the corner of the sheet and tuck it under the side.
- On the lower left corner, pull the corner tight and tuck it around the underside of the bed. Pull up the corner of the sheet and tuck it under the side.
- On the right side, fold down the top of the flat top sheet about 12 inches.
- On the left side, fold down the top of the flat top sheet about 12 inches.
- On the right side, tuck in the sheet on the side.
- On the left side, tuck in the sheet on the side.
- On the right side, put the pillow case on the pillow.
- On the left side, put the pillow case on the pillow.
- On the right side, place the pillow at the top of the bed.
- On the left side, place the pillow at the top of the bed.
- On the lower right side, pull the comforter tight over the edge.
- On the lower left side, pull the comforter tight over the edge.
- On the right side, pull the comforter up and over the pillow.
- On the left side, pull the comforter up and over the pillow.
- If no,
- On the right side, pull up the flat top sheet to the top of the bed.
- On the left side, pull up the flat top sheet to the top of the bed.
- On the right side, fold down the top of the flat top sheet about 12 inches.
- On the left side, fold down the top of the flat top sheet about 12 inches.
- On the right side, tuck in the sheet on the side.
- On the left side, tuck in the sheet on the side.
- On the right side, place the pillow at the top of the bed.
- On the left side, place the pillow at the top of the bed.
- On the right side, pull the comforter up and over the pillow.
- On the left side, pull the comforter up and over the pillow.
Repetition
Repeative processes happen until a certain condition is met. That condition is also binary - yes or no.
Let's look at a re-write of the above algorithm by incorporating repetition.
- Do the sheets need to be changed?
- If yes,
- Remove the comforter from the bed.
- Repeat two times
- remove the pillow case for the pillow.
- Remove the flat sheet.
- Remove the fitted sheet.
- Get fresh sheets
- Repeat four times
- On one of the corners, put the fitted sheet around the corner.
- Move to the next corner.
- Repeat two times
- On one of the upper corners, lay out the flat sheet, pulling it up to the corner.
- On one of the lower corners, lay otut the flat sheet, pulling it down aroudn the corner straightening it out.
- Move to the other side of the bed.
- Repeat two times
- On one of the lower corners, pull the corner tight and tuck it around the underside of the bed. Pull up the corner of the sheet and tuck it under the side.
- Move to the other lower corner.
- Repeat two times
- On one side, fold down the top of the flat top sheet about 12 inches.
- Move to the other side of the bed.
- Repeat two times
- On one side, tuck in the sheet on the side.
- Move to the other side of the bed.
- Repeat two times
- Put a pillow case on a pillow.
- Repeat two times
- Place a pillow at the top of the bed.
- Repeat two times
- On one lower side, pull the comforter tight over the edge.
- On the same side upper corner, pull the comforter up and over the pillow.
- Move to the other side of the bed.
- If no,
- Repeat two times
- On one side, pull up the flat top sheet to the top of the bed.
- On the same side, fold down the top of the flat top sheet about 12 inches.
- Tuck in the sheet on the side.
- Move to the other side of the bed.
- Repeat two times
- Place a pillow at the top of the bed.
- Repeat two times
- On one side, pull the comforter up and over the pillow.
- Move to the other side of the bed.
Algorithms can be written in multiple ways. We have been writing our algorithms using pseudocode. Using pseudocode allows you to write the algorithm using your own words and often in steps.
Flowcharts
You can also represent algorithms using flowchart symbols. Flowcharting can be considered outdated and too rigid, but for some people, it provides a more structured way to look at the algorithm that would assist them later when translating an algorithm into program code.
We can use flowchart symbols to create this same algorithm. In the flowchart, the following symbols are used:
Symbol |
Description |
Example |
Explanation of Example |
|
Used to start and end the algorithm. |
|
This symbol is used to start your algorithm. |
|
Used to represent a single step. |
|
Calculates the quotient of dividing totalTemp by 30 and averageTemp is assigned the result. |
|
Used to make a decision, with the steps to take when it is true, going to the right and the steps to take when it is false, going to the left. |
|
Checks to see if your birthday (YourBday) is today. If it is, then it adds a free cookie (FreeCookie) reward to your account. |
The diamond symbol is also used to represent the decision for whether or not the statements should repeat.
| |
Checks to see if the number of repetitions (numReps) is less than or equal to 10. If it is, then it simulates a jumping jack (jumpingJack) and increases the number of repetitions (numReps) by 1. |
|
Used to create a path for the algorithm to follow from one step to the next step. |
Brushing Teeth Algorithm
Pseudocode Example
For example, the following represents an example of pseudocode that could be used to brush your teeth in the morning.
- Get out the toothbrush and toothpaste and uncap it.
- Wet the toothbrush with water from the sink faucet.
- Put about a quarter inch of toothpaste on the bristles of the toothbrush.
- For each one of your teeth:
- scrub in circular motions on the front, back, and top of your tooth
- Spit excess toothpaste into the sink
- Rinse your tooth brush
Flowchart Example
The following is the flowchart representation of the algorithm written in pseudocode above.
Resources