Repetition Structures Overview
Repetition structures are also known as “loops”. Loops are used when we want our program to repeat a task. Looping through the repetition structure, is also known as “iterating”.
There are several types of loops, but they fall into four basic categories...pre-test, post-test, counter-controlled, and condition-controlled. In fact, most loops fall into more than one category and it is important to pick the correct loop for the task.
- Pre-test loops test the condition before the loop ever executes. In fact, if the condition does not reconcile to
true, the pre-test loop will never execute. - Post-test loops allow the loop to run before testing the condition. In other words, the code inside of the loop body will run at least once before the condition ever gets tested.
- Counter-controlled loops execute a specific number of times. The variable that tracks the loop iterations is called an iterator.
- Condition-controlled loops use a sentinel to “watch” for a change in the condition, signaling it is time to stop the loop. This type of loop can put the end-user in charge.
Remember to study the pseudocode on this page. When you need a loop and you are writing the 5-Step Solution Algorithm, you can code the loops as shown on this page.
Pre-test Loops
In a pre-test loop, the condition is tested first. The code within the loop is run IF AND ONLY IF the condition reconciles to true.
The while loop is a pre-test loop. Remember, the condition is tested first. That is why it is imperative to establish your condition before the while loop signature. The code within the loop body is executed only if the condition reconciles to true.
//establish your condition BEFORE the loop
WHILE (condition is true){
//write code statements INCLUDING one to
//change the condition inside the loop body!
}
In a pre-test loop, the condition is always checked first. Only if the condition is true will the code inside the loop run. Assuming your condition reconciles to true, a pre-test loop performs as such:
- After all the statements in the body of the loop are run the condition is tested again.
- If the condition is still
truethen the code in the loop body runs again. - This behavior happens over and over until the condition becomes
false.
If the condition is never true - the statements in the loop body NEVER run! The loop body is the block of code between the curly braces.
IMPORTANT! Make sure you code the condition BEFORE the while loop signature. Also, it is imperative to write code inside the loop body that will change the condition.
The for loop is also a pre-test loop, however, its signature is more complicated than the while loop so I omitted it in this overview. We will study both loops in detail in another chapter.
Post-test Loops
With the post-test loop, the code runs first, and THEN the condition is tested. The code is guaranteed to run AT LEAST once.
The do/while and do/until loops are both post-test loops. However, the do/until is not used in many modern programming languages. For that reason, we will focus on the do/while loop.
In the do/while loop, the code within the loop body runs first and then the condition is tested. As long as the condition reconciles to true the code will continue to execute.
DO{
//establish your condition inside the loop
//change your condition inside the loop body!
}WHILE (condition is true)
With the do/while loop the code block is run before the condition is tested. This allows us to guarantee that the statements will run at least one time. It also means that the order of your statements within the body of the loop is extremely important.
Interesting...that means the code statements within the body of the loop will run at least once - even if the condition is never true!
It is only after the code block is run that the condition gets tested. If the condition is true then the code block will run again. This behavior continues as long as the condition is true.
Counter-Controlled Loops
A counter-controlled loop is used to repeat the statement block a specific number of times. In other words, for a specific count.
The concept of counted looping is simple, you do something a specified number of times. However, keep in mind, there are different ways to specify the number. For instance the number does not need to be a specific number like 57. A specific number of times can mean the loop should run as long as there are elements left in an array or as long as there are rows left in a result set. You may not know the exact number, but the array or result set does.
The for loop is a counter-controlled loop. Also, if coded as such, the while loop is a counter-controlled loop. If you recall, both these loops are also considered pre-test loops.
set counter to zero //this establishes your condition
WHILE (counter < 10){
//write code statements
add 1 to counter //this changes the condition
}
Condition-Controlled Loops
When you want to repeat code, but do not know how many times, you can code the while loop or do/while loop to be a condition-controlled loop.
Here is a scenario...The end-user needs to enter data. You could ask the end-user in advance how many items do they need to enter, but that would not be very user friendly. Instead, you can create a condition-controlled while loop or do/while loop that allows the end-user to enter a specific number, letter, or word when their data entry is complete.
Your program keeps an eye out for this “sentinel” and the loop stops when the sentinel value is detected.
//establish your condition BEFORE the loop
WHILE (entry not equal to quit){
//write code statements that allows
//the condition/sentinel to change inside the loop body!
}
DO{
//establish your condition inside the loop
//change your condition/sentinel inside the loop body!
}WHILE (entry not equal to quit)
Notice the negative approach to the syntax above. Also notice there is no counter to increment. You want the code to run as long as the end-user did NOT signal they are finished. You must allow the sentinel to be changed inside the loop body or your loop will not end.
It is important to remember that the condition in all loops is (re)checked with each iteration of the loop.
Infinite Loops
Loops are great, but if you are not careful you will create the dreaded infinite loop! An infinite loop is a loop that does not end. Typically this is due to logic errors. Below are some common situations that produce an infinite loop:
- You are using a counter-controlled
whileand forget to increment your iterator variable. - You forget to change your condition inside a loop.
- You are using a
forloop and the relationships do not logically match.
What You Learned
You learned that both the for loop and while loop are pre-test loops. The while loop can be coded as a counter-controlled loop whereas the for loop is always considered a counter-controlled loop.
You also learned the condition-controlled loop is handy when you want the end-user to be in charge of when the loop should end. Your code in the condition-controlled loop watches for a number or phrase that signals the end-user is finished.
You learned it was important to allow the condition to change so that it can become not true. If the condition was always true, the loop would be an infinite loop. You also learned that a post-test loop is guaranteed to run at least once. Lastly, you learned how to structure the while and do/while loops when writing a 5-Step Solution Algorithm.
What's Next
In our next learning activity, we will explore each type of counter-controlled loop in greater detail.