Skip Top Navigation Bar

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.

Decisions

When the computer make decisions, they are binary - yes or no. Some steps only happen when the conditions or the decision is true or yes, and others only happen when the condition is false or no. Sometimes there are additional decision to make along the way as well.

In the making the bed algorithm, maybe there is a decision at the start to determine whether the sheets need to be changed or not.

Repetition

Repetition is when the same process happens more than once. Repetition can be set up so that the repeative processes continues until a condition is met or while a condition is met. The conditions are binary - yes or no.

Your Turn

Grab a Piece of Paper, Let's Try It!

Take a look at the following algorithm for steps to make a bed.

  • Are there decisions that should be included?
  • Are there repeative steps used in this process?
  1. On both sides, pull up the flat top sheet to the top of the bed.
  2. On both sides, tuck in the sheet on the side.
  3. On both sides, place the pillow at the top of the bed.
  4. On both sides, pull the comforter up and over the pillow.

Decisions: Do the sheets need to be changed?

Repetition: Doing things on all four corners or both sides of the bed.

Let's look at a re-write of the above algorithm by incorporating repetition.

  1. Do the sheets need to be changed?
    • If yes,
      1. Remove the comforter from the bed.
      2. Repeat two times
        • remove the pillow case for the pillow.
      3. Remove both the flat and fitted sheets.
      4. Get fresh sheets
      5. Repeat four times
        • On one of the corners, put the fitted sheet around the corner.
        • Move to the next corner.
      6. 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 out the flat sheet, pulling it down around the corner straightening it out.
        • Move to the other side of the bed.
      7. 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.
      8. Repeat two times
        • On one side, tuck in the sheet on the side.
        • Move to the other side of the bed.
      9. Repeat two times
        • Put a pillow case on a pillow.
        • Place a pillow at the top of the bed.
      10. 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,
      1. Repeat two times
        • On one side, pull up the flat top sheet to the top of the bed.
        • Tuck in the sheet on the side.
        • Move to the other side of the bed.
      2. Repeat two times
        • Place a pillow at the top of the bed.
        • Pull the comforter up and over the pillow.
        • Move to the other side of the bed.

Resources

Next Learn Tutorial

Learn: Representing Algorithms with Flowcharts