In this lesson, we look at what a programming algorithm is – and what it isn’t. We also look at an example of a common algorithm shown as both a numbered list and a flowchart, after which we briefly analyze what it does.
What Is a Programming Algorithm?
So, what is a programming algorithm? You can think of a programming algorithm as a recipe that describes the exact steps needed for the computer to solve a problem or reach a goal. We’ve all seen food recipes – they list the ingredients needed and a set of steps for how to make the described meal.
Well, an algorithm is just like that. In computer lingo, the word for a recipe is a procedure, and the ingredients are called inputs. Your computer looks at your procedure, follows it to the letter, and you get to see the results, which are called outputs. A programming algorithm describes how to do something, and your computer will do it exactly that way every time. Well, it will once you convert your algorithm into a language it understands!However, it’s important to note that a programming algorithm is not computer code. It’s written in simple English (or whatever the programmer speaks).
It doesn’t beat around the bush–it has a start, a middle, and an end. In fact, you will probably label the first step ‘start’ and the last step ‘end.’ It includes only what you need to carry out the task. It does not include anything unclear, often called ambiguous in computer lingo, that someone reading it might wonder about.
It always leads to a solution and tries to be the most efficient solution we can think up. It’s often a good idea to number the steps, but you don’t have to. Instead of numbered steps, some folks use indentation and write in pseudocode, which is a semi-programming language used to describe the steps in an algorithm.
But, we won’t use that here since simplicity is the main thing. Other folks just use a diagram called a flowchart, which we will discuss soon.
Programming Algorithm Example
Okay, you probably wish you could see an example, right? So, what exactly does an algorithm in programming look like? Well, asking a user for an email address is probably one of the most common tasks a web-based program might need to do, so that is what we will use here for an example. An algorithm can be written as a list of steps using text or as a picture with shapes and arrows called a flowchart.
We will make one of each which you will see here:
Wasn’t that easy? Notice how the top of our example is just a numbered list of steps using plain English, stating exactly what we want the procedure to do (no more, no less). The bottom is the very same algorithm, but this time, we used shapes and arrows in a flowchart (like a map of the route), so that a reader can visualize the journey.
That’s a nice thing here, because in one of our steps (step 7) a decision must be made and, depending on the result of that decision, our steps may not go in order from start to end.Okay! Let’s take a quick run through our little recipe:1. Step 1 is really just a reminder that this is a procedure with a beginning and an end.2.
In step 2, we make a place in the computer to store what the user types in, also called a variable3. In step 3, we clear this variable because we might need to use it again and don’t want the old contents mixed in with the new.4.
In step 4, we prompt the user for an email address5. In step 5, we stick it in our nifty variable.6. In step 6, we tell our computer to take a close look at this email address– is it really an email address?7. In step 7, we make a decision; if we got a valid email address, proceed to step 8 (the End), and if not, well, we’d better go back and get one that is!8.
Step 8 – EndAs you can see, if the email address is invalid, we jump back to step 3, clear the old one out and stash the new one there, and then continue as usual in the hope we have a good one now. If not; well, this will keep going until we do. You are probably thinking we should add an escape route here, and you’d be right! No one wants to get stuck in an infinite loop. Maybe you can add that for us? Otherwise, that’s it!
Was that easy or what? Great! You just learned what a programming algorithm is, saw an example of what a simple algorithm looks like, and then we ran through a quick analysis of how an algorithm works. Now, let’s review.A programming algorithm is a computer procedure that is a lot like a recipe (called a procedure) and tells your computer precisely what steps to take to solve a problem or reach a goal.
The ingredients are called inputs, while the results are called the outputs. An algorithm is not computer code; it’s written in plain English and may be in the form of a flowchart with shapes and arrows, a numbered list, or pseudocode (a semi-programming language). It doesn’t beat around the bush. It’s very clear and efficient, and it has a start, middle, and end.
We looked at a simple example of an algorithm that does some preparation, asks a user for an email address, and decides what to do. Depending on whether it’s a valid email address or not, we might have to repeat some steps until we can get all the way to the end without any problems.Do you feel more familiar with algorithms for programming now? Terrific! Why don’t you try and write one just for fun? After all, it’s just a recipe.
Programming algorithm – a recipe that describes the exact steps needed for the computer to solve a problem or reach a goalProcedure – the steps in a computer’s ‘recipe’Inputs – the ingredients for a computer’s ‘recipe’Outputs – the results of a programming algorithmPseudocode – a semi-programming language used to describe the steps in an algorithm
Watch the video lesson and learn about algorithm programming, then assess your ability to:
- Verbalize the definition of the term ‘programming algorithm’ and discuss its use
- Identify examples of programming algorithms
- Write an algorithm for programming