Often, when programming, we need to perform a series of steps multiple times.
For example, think of all of the steps you need to do to walk. You have to lift your foot off the ground. You have to balance yourself on the one foot that is on the ground. You have to extend your leg to move forward, and then lower the foot, and “fall” toward the ground. All of those steps are just to half way walk, you’ve got to move your other leg as well. And you have to do this for each step you take as you walk around campus, your house, the store, etc.
There are two ways we could do this.
First we could just repeat the steps the number of times we need to perform them. However, if we have to perform those steps either a large number of times, or an unknown number of times, this becomes a real challenge. Could you imagine copying and pasting all of those steps, to take a step, if you have to walk 100 steps, or 1000? And what if you don’t know how far you have to go?
So the second option, and the one we use, is to use a repetition structure, better known as a loop.
All loops work in general on the premise of we check to see if we need to do this series of steps again, and if so, we “run our loop body”. That is, we execute every step within the series of steps that makes up our loop body. Then we check to see if we need to do it again.
Now this entire process may seem complicated, but actually, its fairly simple to code, which is why we use it, and it’s simple to create in our flowcharts.
There are two main types of loops that we will utilize. Conditional loops and counting loops. With a flowchart, the way we create them is the same – but we’re going to change how we check to see if they need to run again. Let’s take a look at them.
Conditional Loops
Conditional loops repeat a series of statements while some condition is true. This series of statements could be a single command, or dozens, or even hundreds of commands.
Think of our example from before, where we’re telling a robot how to walk. We want it to walk to the end of the hall, but don’t know how many steps it will take. So we tell it to walk until it reaches the end.
Not if this sounds like making a decision, you are correct. As you look to create a flowchart for a loop, you will once again use the decision symbol with your condition inside of it. It can be a simple condition, or a complex condition with Boolean operators. The difference is, one branch of flowlines, will loop back to right before the decision statement, so that you can check to see if you need to run your loop body again. The other flowline exits out of the condition.
Design Tip: Typically, we use the Boolean true to run our loops, but you don’t have to. It’s just easier to write on decisions that way because of how our brains work.
Consider an example of what it would look like to read an entire file in, one line at a time. A flow chart should be able to do that no problem. You can ask “While there are more lines in the file” or “While we’ve not reached the end of the file”. The first question flows more naturally for us.
Counting Loops
Counting loops are very similar to conditional loops. In fact, at a quick glance, you might think they are the same. However, you would be mistaken.
The difference is that a counting loop utilizes a counter, to keep track of how many times the loop has occurred. After a given number of times, the loop will end, or as we sometimes like to say, terminate.
Each time you go through the loop, your counter will need to increment by a given number. Most of the time, we increment by one. However, the counter can be used for different things, not just counting, so we might increment by twos, fives, tens, etc.
Counting loops are great if we know how many times something will occur. For example, we know we have 20 students in a classroom, and we need to calculate the average grade. So we can add up a series of numbers for each student, and then calculate the averaged value.
Note: With the previous example, you might even see a nest loop. That is a loop with in a loop. So as you look at each student, they might have multiple grades that need to be averaged.
Repeating Statements to Solve a Problem was originally found on Access 2 Learn
3 Comments
Comments are closed.